finish translating

This commit is contained in:
Asche 2019-09-20 16:58:37 +08:00 committed by GitHub
parent b6e38ad224
commit dba2a01b06
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,63 +7,67 @@
[#]: via: (https://www.2daygeek.com/linux-sed-to-find-and-replace-string-in-files/)
[#]: author: (Magesh Maruthamuthu https://www.2daygeek.com/author/magesh/)
How to Find and Replace a String in File Using the sed Command in Linux
在Linux中使用sed命令如何查找和替换文件中的字符串
======
When you are working on text files you may need to find and replace a string in the file.
Sed command is mostly used to replace the text in a file.
当你在使用文本文件时,很可能需要查找和替换文件中的字符串。
This can be done using the sed command and awk command in Linux.
Sed命令主要用于替换一个文件中的文本。
In this tutorial, we will show you how to do this using the sed command and then show about the awk command.
在Linux中这可以通过使用sed命令和awk命令来完成。
### What is sed Command
Sed command stands for Stream Editor, It is used to perform basic text manipulation in Linux. It could perform various functions such as search, find, modify, insert or delete files.
Also, its performing complex regular expression pattern matching.
It can be used for the following purpose.
* To find and replace matches with a given format.
* To find and replace specific lines that match a given format.
* To find and replace the entire line that matches the given format.
* To search and replace two different patterns simultaneously.
在本教程中我们将告诉你使用sed命令如何做到这一点然后讨论讨论awk命令相关的。
### sed命令是什么
The fifteen examples listed in this article will help you to master in the sed command.
sed命令表示Stream Editor流编辑器用来在Linux上执行基本的文本操作。 它可以执行各种功能,如搜索,查找,修改,插入或删除文件。
If you want to remove a line from a file using the Sed command, go to the following article.
此外,它也可以执行复杂的正则表达式匹配。
**`Note:`** Since this is a demonstration article, we use the sed command without the `-i` option, which removes lines and prints the contents of the file in the Linux terminal.
But if you want to remove lines from the source file in the real environment, use the `-i` option with the sed command.
它可用于以下目的:
Common Syntax for sed to replace a string.
* 查找和替换匹配给定的格式的内容。
* 在指定行查找和替换匹配给定的格式的内容。
* 在所有行查找和替换匹配给定的格式的内容。
* 搜索并同时替换两种不同的模式。
本文列出的十五个例子可以帮助你掌握sed命令。
如果要使用sed命令删除文件中的行去下面的文章。
** `注意:`**由于这是一篇演示文章,我们使用不带`-i '选项的sed命令该命令在Linux终端中删除行并打印文件内容。
但是,在实际环境中如果你想删除源文件中的行,使用带`-i`选项的sed命令。
常见的sed替换字符串的语法。
```
sed -i 's/Search_String/Replacement_String/g' Input_File
```
First we need to understand sed syntax to do this. See details about it.
* `sed:` Its a Linux command.
* `-i:` Its one of the option for sed and what it does? By default sed print the results to the standard output. When you add this option with sed then it will edit files in place. A backup of the original file will be created when you add a suffix (For ex, -i.bak
* `s:` The s is the substitute command.
* `Search_String:` To search a given string or regular expression.
* `Replacement_String:` The replacement string.
* `g:` Global replacement flag. By default, the sed command replaces the first occurrence of the pattern in each line and it wont replace the other occurrence in the line. But, all occurrences will be replaced when the replacement flag is provided
* `/` Delimiter character.
* `Input_File:` The filename that you want to perform the action.
首先我们需要了解sed语法来做到这一点。 请参阅有关的细节。
* `sed:` 这是一个Linux命令。
* `-i:` 这是sed命令的一个选项它有啥作用 默认情况下sed打印结果到标准输出。 当您添加使用sed这个选项时那么它会在适当的位置修改文件。 当您添加一个后缀(比如, -i.bak原始文件的备份将就会被创建。
* `s:` 字母s是一个替换命令。
* `Search_String:` 搜索一个给定的字符串或正则表达式。
* `Replacement_String:` 替换的字符串。
* `g:` 全局替换标志。 默认情况下sed命令替换每一行第一次出现的pattern模式它不会替换行中的其他匹配结果。 但是,提供了该置换标志时,所有匹配都将被替换。
* `/` 分界符。
* `Input_File:` 要执行操作的文件名。
Let us look at some examples of commonly used with sed command to search and convert text in files.
We have created the below file for demonstration purposes.
让我们来看看文件中用sed命令来搜索和转换文本的一些常用例子。
我们已经创建了用于演示的以下文件。
```
# cat sed-test.txt
@ -74,9 +78,10 @@ linux /bin/bash CentOS Linux OS
Linux is free and opensource operating system
```
### 1) How to Find and Replace the “first” Event of the Pattern on a Line
### 1) 在一行中如何查找和替换Pattern模式的“第一次”匹配
The below sed command replaces the word **unix** with **linux** in the file. This only changes the first instance of the pattern on each line.
下面的sed命令用**linux**替换文件中的**unix**。 这仅仅改变了每一行pattern模式的第一个实例。
```
# sed 's/unix/linux/' sed-test.txt
@ -88,11 +93,11 @@ linux /bin/bash CentOS Linux OS
Linux is free and opensource operating system
```
### 2) How to Find and Replace the “Nth” Occurrence of the Pattern on a Line
### 2) 如何查找和替换每一行中“第N次”出现的模式
Use the /1,/2,../n flags to replace the corresponding occurrence of a pattern in a line.
在行中使用/1,/2,../n标志来代替相应的匹配。
The below sed command replaces the second instance of the “unix” pattern with “linux” in a line.
下面的sed命令在一行中用“linux”来替换“unix”模式的第二个实例。
```
# sed 's/unix/linux/2' sed-test.txt
@ -104,10 +109,12 @@ linux /bin/bash CentOS Linux OS
Linux is free and opensource operating system
```
### 3) How to Search and Replace all Instances of the Pattern in a Line
### 3) 在一行中如何搜索和替换模式的所有实例
The below sed command replaces all instances of the “unix” format with “Linux” on the line because “g” means a global replacement.
下面的sed命令用“Linux”替换“unix”格式的所有实例因为“G”是指一个全局的替换标志。
```
# sed 's/unix/linux/g' sed-test.txt
@ -118,9 +125,8 @@ linux /bin/bash CentOS Linux OS
Linux is free and opensource operating system
```
### 4) How to Find and Replace the Pattern for all Instances in a Line from the “Nth” Event
The below sed command replaces all the patterns from the “Nth” instance of a pattern in a line.
### 4) 在一行的“第N个”匹配中如何查找和替换模式的所有实例
下面的sed命令在一行中且从模式的“第n个”实例中替换所有的模式。
```
# sed 's/unix/linux/2g' sed-test.txt
@ -132,9 +138,9 @@ linux /bin/bash CentOS Linux OS
Linux is free and opensource operating system
```
### 5) Search and Replace the pattern on a specific line number
### 5) 在特定的行号搜索和替换模式
You can able to replace the string on a specific line number. The below sed command replaces the pattern “unix” with “linux” only on the 3rd line.
你可以替换特定行号中的字符串。 下面的sed命令用“linux”仅替换第三行的“unix”模式。
```
# sed '3 s/unix/linux/' sed-test.txt
@ -146,12 +152,16 @@ linux /bin/bash CentOS Linux OS
Linux is free and opensource operating system
```
### 6) How to Find and Replace Pattern in a Range of Lines
### 6) 在特定范围行号间搜索和替换模式
You can specify the range of line numbers to replace the string.
The below sed command replaces the “Unix” pattern with “Linux” with lines 1 through 3.
您可以指定行号的范围,以替换字符串。
下面的sed命令在1到3行间用“Linux”替换“Unix”模式。
```
# sed '1,3 s/unix/linux/' sed-test.txt
@ -162,12 +172,11 @@ linux /bin/bash CentOS Linux OS
Linux is free and opensource operating system
```
### 7) How to Find and Change the pattern in the Last Line
### 7) 如何查找和修改最后一行的模式
The below sed command allows you to replace the matching string only in the last line.
The below sed command replaces the “Linux” pattern with “Unix” only on the last line.
下面的sed命令允许您只在最后一行更换匹配的字符串。
下面的sed命令只在最后一行用“Unix”替换“Linux”模式。
```
# sed '$ s/Linux/Unix/' sed-test.txt
@ -178,10 +187,8 @@ linux /bin/bash CentOS Linux OS
Unix is free and opensource operating system
```
### 8) How to Find and Replace the Pattern with only Right Word in a Line
As you might have noticed, the substring “linuxunix” is replaced with “linuxlinux” in the 6th example. If you want to replace only the right matching word, use the word-boundary expression “\b” on both ends of the search string.
### 8) 在一行中如何只查找和替换正确的模式匹配
你可能已经注意到子串“linuxunix”被替换为在第6例中的“linuxlinux”。 如果您只想更改正确的匹配词,用这个边界符“\b”上的搜索串的两端。
```
# sed '1,3 s/\bunix\b/linux/' sed-test.txt
@ -192,10 +199,9 @@ linux /bin/bash CentOS Linux OS
Linux is free and opensource operating system
```
### 9) How to Search and Replaces the pattern with case insensitive
Everyone knows that Linux is case sensitive. To make the pattern match with case insensitive, use the I flag.
### 9) 如何以不区分大小写来搜索与替换模式
大家都知道Linux是区分大小写的。 为了与不区分大小写的模式匹配使用I标志。
```
# sed 's/unix/linux/gI' sed-test.txt
@ -206,11 +212,13 @@ linux /bin/bash CentOS Linux OS
Linux is free and opensource operating system
```
### 10) How to Find and Replace a String that Contains the Delimiter Character
### 10) 如何查找和替换包含分隔符的字符串
When you search and replace for a string with the delimiter character, we need to use the backslash “\” to escape the slash.
In this example, we are going to replaces the “/bin/bash” with “/usr/bin/fish”.
当您搜索和替换含分隔符的字符串时,我们需要用反斜杠“\”来取消转义。
在这个例子中,我们将用“/usr/bin/fish”来替换“/bin/bash”。
```
# sed 's/\/bin\/bash/\/usr\/bin\/fish/g' sed-test.txt
@ -222,7 +230,7 @@ linux /usr/bin/fish CentOS Linux OS
Linux is free and opensource operating system
```
The above sed command works as expected, but it looks bad. To simplify this, most of the people will use the vertical bar “|”. So, I advise you to go with it.
上述sed命令按预期工作但它看起来来很糟糕。 为了简化,大部分的人会用竖线“|”。 所以,我建议你去用它。
```
# sed 's|/bin/bash|/usr/bin/fish/|g' sed-test.txt
@ -234,9 +242,9 @@ linux /usr/bin/fish/ CentOS Linux OS
Linux is free and opensource operating system
```
### 11) How to Find and Replaces Digits with a Given Pattern
### 11) 如何以给定的模式来查找和替换数字
Similarly, digits can be replaced with pattern. The below sed command replaces all digits with “[0-9]” “number” pattern.
类似地,数字可以用模式来代替。 下面的sed命令替换所有数字以“[0-9]”“number”的模式。
```
# sed 's/[0-9]/number/g' sed-test.txt
@ -248,9 +256,8 @@ linux /bin/bash CentOS Linux OS
Linux is free and opensource operating system
```
### 12) How to Find and Replace only two Digit Numbers with Pattern
If you want to replace the two digit numbers with the pattern, use the sed command below.
### 12) 如何用模式仅查找和替换两个数字
如果你想用模式来代替两位数字使用下面的sed命令。
```
# sed 's/\b[0-9]\{2\}\b/number/g' sed-test.txt
@ -262,14 +269,13 @@ linux /bin/bash CentOS Linux OS
Linux is free and opensource operating system
```
### 13) How to Print only Replaced Lines with the sed Command
If you want to display only the changed lines, use the below sed command.
* p It prints the replaced line twice on the terminal.
* n It suppresses the duplicate rows generated by the “p” flag.
### 13) 如何用sed命令仅打印被替换的行
如果你想显示仅更改的行使用下面的sed命令。
* p - 它输出替换的行在终端上两次。
* n - 它抑制由“p”标志所产生的重复行。
```
# sed -n 's/Unix/Linux/p' sed-test.txt
@ -278,11 +284,11 @@ If you want to display only the changed lines, use the below sed command.
3 linuxunix LinuxLinux
```
### 14) How to Run Multiple sed Commands at Once
### 14) 如何同时运行多个sed命令
The following sed command detect and replaces two different patterns simultaneously.
以下sed命令检测和同时置换两个不同的模式。
The below sed command searches for “linuxunix” and “CentOS” pattern, replacing them with “LINUXUNIX” and “RHEL8” at a time.
下面的sed命令搜索为“linuxunix”和“CentOS的”模式用“LINUXUNIX”和“RHEL8”一次性更换他们。
```
# sed -e 's/linuxunix/LINUXUNIX/g' -e 's/CentOS/RHEL8/g' sed-test.txt
@ -294,10 +300,9 @@ linux /bin/bash RHEL8 Linux OS
Linux is free and opensource operating system
```
The following sed command search for two different patterns and replaces them with one string at a time.
The below sed command searches for “linuxunix” and “CentOS” pattern, replacing them with “Fedora30” at a time.
下面的sed命令搜索替换两个不同的模式每次以一个字符串。
以下sed的命令搜索 为“linuxunix”和“CentOS的”模式用“Fedora30”替换它们。
```
# sed -e 's/\(linuxunix\|CentOS\)/Fedora30/g' sed-test.txt
@ -308,9 +313,10 @@ linux /bin/bash Fedora30 Linux OS
Linux is free and opensource operating system
```
### 15) How to Find and Replace the Entire Line if the Given Pattern Matches
### 15) 如何查找和替换整个行,如果给定的模式匹配
If the pattern matches, you can use the sed command to replace the entire line with the new line. This can be done using the “C” flag.
如果模式匹配可以使用sed命令用新行来代替整行。 这可以通过使用“C”标志来完成。
```
# sed '/OS/ c New Line' sed-test.txt
@ -322,11 +328,11 @@ New Line
Linux is free and opensource operating system
```
### 16) How to Search and Replace lines that Matches a Pattern
### 16) 如何搜索和替换相匹配的模式行
You can specify a pattern for the sed command to fit on a line. In the event of pattern matching, the sed command searches for the string to be replaced.
在一行中您可以为sed命令指定一种模式以适应。 在模式匹配的情况下sed命令搜索要被替换的字符串。
The below sed command first looks for lines that have the “OS” pattern, then replaces the word “Linux” with “ArchLinux”.
下面的sed命令首先查找具有“OS”模式的行然后用“ArchLinux”替换单词“Linux”。
```
# sed '/OS/ s/Linux/ArchLinux/' sed-test.txt