Merge pull request #6091 from geekpi/master

translated
This commit is contained in:
geekpi 2017-09-27 08:42:33 +08:00 committed by GitHub
commit 3f16641d8f
2 changed files with 173 additions and 175 deletions

View File

@ -1,175 +0,0 @@
translating---geekpi
LFCS sed Command
=====================
Another useful command for the Linux Foundation Certified SysAdmin (LFCS) is the 'sed' Command. The initials stand for 'Streaming EDitor'.
The 'sed' Command is an editor that edits the file as a stream. To stream a file is to pass it from another command using pipes (> or |) or load it directly into 'sed'.
The command works like any other editor except that the file is not displayed and allowed to be edited visually. Commands are passed to 'sed' to manipulate the stream.
With 'sed' there are five basic things which can be done. Of course, with 'sed' being so powerful there are more advanced functions, but you only need to concentrate on the basic five. The five types of functionality are as follows:
1. Search
2. Replace
3. Delete
4. Adding
5. Change/Transform
Before getting into the command parameters we need to look over the basic syntax.
**Syntax**
The syntax for the 'sed' command is:
_sed [options] commands [file-to-edit]_
The options will be covered in this article in the appropriate sections. The commands are the search and replacement patterns which can be Regex Expressions. Read on to see how 'sed' works and learn the basic commands. As I mentioned before, 'sed' is a very powerful tool and has more options available than I will cover in the article.
**Example File**
If you open a Terminal you can create a file to be used for the 'sed' examples. Perform the following commands:
_cd ~
grep --help >grephelp.txt_
You should now have a file named 'grephelp.txt' in your HOME folder. The contents of the file are the help instructions for the command 'grep'.
**Search**
Searching for specific strings is a common ability of editors and performing searches in 'sed' is no exception.
Searches can be performed to find a string in a file. Let's look at a basic search.
If we wanted to search through our example file for the word 'PATTERN' we would use the command and see the results in Figure 1:
_sed -n 's/PATTERN/PATTERN/p' grephelp.txt_
**NOTE:** If you cut and paste the command make sure you replace the single quotes with that of a standard single quote on your keyboard.
![Figure 01.jpg](https://www.linux.org/attachments/figure-01-jpg.684/)
**FIGURE 1**
The parameter '-n' is used to suppress the printing of each line automatically. These lines include lines which do not include the search pattern. By using the '-n' the lines with a matching result will be displayed. Every line which is streamed into 'sed' will be printed to the standard out (stdout). If you run the above command without the '-n' option you will have a line for every line in the original file as well as a duplicate line for each match.
The file name to search is 'grephelp.txt' which we created in the 'Example File' section.
The portion left is  _'s/PATTERN/PATTERN/p'_ . There are basically four sections to this section. The first section 's' specifies to perform a substitution, or a search and replace.
The second and third part of the remaining portion are the patterns. The first is the pattern to search for and the last is the pattern to replace the matching string within the stream. In this case we are finding the string 'PATTERN' and replacing it with 'PATTERN'. By finding and replacing the same string we are not changing the file at all, even on the screen.
The last command is 'p'. The 'p' command specifies to print the new line after the substitution is made. Of course, there is no change since the substitution is the same string. Since we are suppressing printing lines with -n parameter only the changed lines will be printed with the p command.
The whole command allows us to perform a search and see the matching results.
**Replace**
When searching for specific strings you may want to replace the matching string with a new one. Replacing strings with another is very common.
We can perform the same search with the following command and the results are shown in Figure 2:
_sed -n 's/PATTERN/Pattern/p' grephelp.txt_
![Figure 02.jpg](https://www.linux.org/attachments/figure-02-jpg.685/)
**FIGURE 2**
In this instance, the string 'PATTERN' is changed to 'Pattern' and displayed. If you view the file with the command 'cat grephelp.txt' you will see that the file has not changed. The change was made only to the output on the screen. You could pipe the output to another file with the command:
_sed 's/PATTERN/Pattern/' grephelp.txt > grephelp1.txt_
A new file called 'grephelp1.txt' would now exist which held the changed file. If the 'p' was left in as the fourth option then the problem is that each line that had a string substituted would exist twice in the file. We also drop the '-n' parameter to allow all the lines to print.
Another way to replace the string with the same string is to use the & symbol to represent the search string. For example, the command s/PATTERN/&/p would be the same. We can add to the string, such as adding an S, with the command s/PATTERN/&S/p.
What if we wish to replace only a certain pattern in each line? It is possible to specify the specific occurrence of a match to replace. Of course, the replacement will be a specific numbered occurrence on each line. For example, there are a lot of dashes on the sample file. Some lines have at least two dashes so we can replace the second dash in each line with another character. The command to replace the second dash (-) in each line with an asterisk (*) would be:
sed 's/-/*/2' grephelp.txt
Here, we are performing a substitution with the initial s. The character - is replaced with the *. The 2 shows we are wanting to replace the second instance of the - on each line if it exists. A sample result is shown in Figure 3\. If we left out the command 2 then the first occurrence of a dash is replaced. Not every dash of a line is replaced only the first one.
![Figure 03.jpg](https://www.linux.org/attachments/figure-03-jpg.686/)
**FIGURE 3**
If you want to search and replace all dashes on a line with an asterisk, use the g command:
_sed 's/-/*/g' grephelp.txt_
Commands can also be combined. Lets say you want to change all dashes starting at the second occurrence to the end, the command would be:
_sed 's/-/*/2g' grephelp.txt_
Now every dash from the second to the last on every line is replaced with an asterisk.
**Delete**
Many times during a search you may want to remove the search string completely.
For instance, if you wanted to remove all dashes from the file you could use the command:
_sed s/-//g grephelp.txt_
The replacement string is left as a blank so the matching string is removed or deleted.
**Adding**
When a match is found you can add a line of specific text to make the line stand out for viewing or printing.
If you want the new line to be inserted after the match use the a command followed by the string for the new line. Also include the string to match. For example, we can find a -- and add a line after the matched line. The string on the added line will be double dash before this line.
_sed '/--/ a "double dash before this line"' grephelp.txt_
If you want to place the line before the line containing the matched string use the i command as follows:
_sed '/--/ i "double dash after this line"' grephelp.txt_
**Change/Transform**
If a line needs to be changed or transformed you can use the command c.
Lets say we have a document which has some private information and we need to change any line which contains a specific string. The c command will change the whole line and not just the search string.
Lets say in our example file we want to block out every line which contains the word PATTERN. The changed line will read This line is Top Secret. The command is:
_sed /PATTERN/ c This line is Top Secret grephelp.txt_
A Transformation can be performed to change case of specific letters. For instance, we can change all lower-case a to uppercase A with the command y as follows:
_sed y/a/A/ grephelp.txt_
Multiple letters can be specified such as abdg as in the following command:
_sed y/abdg/ABDG/ grephelp.txt_
Make sure the second set of letters are in the same order as the first or they can be replaced and transformed. For example, the string y/a/D/ would replace all lower-case a with an upper-case D.
**In-place change**
If you actually want to make changes to the file you are using, use the -i option.
For example, to change the word PATTERN to Pattern and have the changes made to the file, the command would be:
sed -i 's/PATTERN/Pattern/' grephelp.txt
The file grephelp.txt will now be changed. The -i option can be used with any of the above commands to change the original file content.
Practice with these commands and make sure you understand them. The sed command is very powerful.
--------------------------------------------------------------------------------
via: https://www.linux.org/threads/lfcs-sed-command.4561/
作者:[Jarret B ][a]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:https://www.linux.org/threads/lfcs-sed-command.4561/

View File

@ -0,0 +1,173 @@
LFCS sed 命令
=====================
Linux 基金会认证系统管理员LFCS的另一个有用的命令是 “sed”最初表示“流式编辑器” Streaming EDitor
“sed” 命令是一个可以将文件作为流进行编辑的编辑器。流式传输文件的方法是从另一个命令使用管道(> 或 |)传递,或将其直接加载到 “sed” 中。
该命令的工作方式与其他编辑器相同,只是文件不显示,也不允许可视化编辑。命令被传递给 “sed” 来操纵流。
用 “sed” 可以做五件基本的事。当然“sed” 如此强大,还有其他高级的功能,但你只需要集中精力在五件基本的事上。五种功能类型如下:
1. 搜索
2. 替换
3. 删除
4. 添加
5. 改变/变换
在深入命令参数之前,我们需要看看基本的语法。
**语法**
“sed” 命令的语法是:
_sed [选项] 命令 [要编辑的文件]_
本文将在适当的部分中介绍这些选项。命令是可以是正则表达式的搜索和替换模式。继续阅读了解 “sed” 如何工作并学习基本命令。正如我之前提到的“sed” 是一个非常强大的工具,有更多的选项可用,我将在本文中介绍。
**示例文件**
如果你打开一个终端,那你可以创建一个用于 “sed” 示例的文件。执行以下命令:
_cd ~
grep --help >grephelp.txt_
你现在应该在 HOME 文件夹中有一个名为 “grephelp.txt” 的文件。该文件的内容是命令 “grep” 的帮助说明。
**搜索**
搜索特定字符串是编辑器的常见功能,在 “sed” 中执行搜索也不例外。
执行搜索以在文件中查找字符串。我们来看一下基本的搜索。
如果我们想在示例文件搜索 “PATTERN” 这个词,我们将使用这个命令,在图 1 中查看结果:
_sed -n 's/PATTERN/PATTERN/p' grephelp.txt_
**注意:** 如果剪切粘贴命令,请确保将单引号替换为键盘上的标准单引号。
![Figure 01.jpg](https://www.linux.org/attachments/figure-01-jpg.684/)
**图 1**
参数 “-n” 用于自动抑制每行的打印。这些行包括不包含搜索模式的行。通过使用 “-n”将显示有匹配结果的行。流入 “sed” 的每一行将被打印到标准输出stdout。如果你不使用 “-n” 选项运行上述命令,你将看到原始文件的每一行,以及每个匹配的重复行。
要搜索的文件名是我们在“示例文件”部分中创建的 “grephelp.txt”。
剩下的部分是 _'s/PATTERN/PATTERN/p'_ 。这一段基本分为四个部分。第一部分的 “s” 指定执行替换,或搜索和替换。
剩下的第二部分和第三部分是模式。第一个是要搜索的模式,最后一个是替换流中匹配字符串的模式。此例中,我们找到字符串 “PATTERN”并用 “PATTERN” 替换。通过查找和替换相同的字符串,我们完全不会更改文件,甚至在屏幕上也一样。
最后一个命令是 “p”。 “p” 指定在替换后打印新行。当然,因为替换的是相同的字符串,所以没有改变。由于我们使用 “-n” 参数抑制打印行,所以更改的行将使用 “p” 命令打印。
完整的命令允许我们执行搜索并查看匹配的结果。
**替换**
当搜索特定字符串时,你可能希望用匹配的字符串替换新字符串。用另一个字符串替换是很常见的。
我们可以使用以下命令执行相同的搜索,结果如图 2 所示:
_sed -n 's/PATTERN/Pattern/p' grephelp.txt_
![Figure 02.jpg](https://www.linux.org/attachments/figure-02-jpg.685/)
**图 2**
在这时,字符串 “PATTERN” 变为 “Pattern” 并显示。如果你使用命令 “cat grephelp.txt” 查看文件,你会看到该文件没有更改。该更改仅对屏幕上的输出进行。你可以使用以下命令将输出通过管道传输到另一个文件:
_sed 's/PATTERN/Pattern/' grephelp.txt > grephelp1.txt_
现在将存在一个名为 “grephelp1.txt” 的新文件,其中保存了更改的文件。如果 “p” 作为第四个选项留下,那么有个问题是每一行被替换的字符串将在文件中重复两次。我们也删除 “-n” 参数以允许所有的行打印。
使用相同字符串替换字符串的另一种方法是使用 “&” 符号来表示搜索字符串。例如,命令 “s/PATTERN/&/p” 效果是一样的。我们可以添加字符串,例如添加 “S” , 可以使用命令 “s/PATTERN/&S/p”。
如果我们希望在每一行中只替换某种模式呢?可以指定要替换的匹配项的特定出现。当然,每一行的替换都是一个特定的编号。例如,示例文件上有很多破折号。一些行至少有两条破折号,所以我们可以用另一个字符代替每一行的第二个破折号。每行用星号(*)替换第二个破折号( - )的命令将是:
sed 's/-/*/2' grephelp.txt
在这里,我们用最初的 “s” 来执行替换。字符 “-” 被替换为 “*”。“2” 表示我们想要替换每行上的第二个 “-”(如果存在)。示例结果如图 3 所示。如果我们忽略了命令 “2”则替换第一次出现的破折号。只有第一个破折号而不是每行的破折号都被替换。
![Figure 03.jpg](https://www.linux.org/attachments/figure-03-jpg.686/)
**图 3**
如果要搜索并替换带有星号的行上的所有破折号,请使用 “g” 命令:
_sed 's/-/*/g' grephelp.txt_
命令也可以组合。假设你想要替换从第二次开始出现的破折号,命令将是:
_sed 's/-/*/2g' grephelp.txt_
现在从第二个开始出现的破折号将被星号取代。
**删除**
搜索过程中有很多时候你可能想要完全删除搜索字符串。
例如,如果要从文件中删除所有破折号,你可以使用以下命令:
_sed s/-//g grephelp.txt_
替换字符串为空白,因此匹配的字符串将被删除。
**添加**
当找到匹配时,你可以添加一行特定的文本,来使这行在浏览或打印中突出。
如果要在匹配后插入新行,那么使用 “a” 命令,后面跟上新行的字符串。还包括要匹配的字符串。例如,我们可以找到一个 “--”,并在匹配的行之后添加一行。新行的字符串将是 “double dash before this line”。
_sed '/--/ a "double dash before this line"' grephelp.txt_
如果要在包含匹配字符串的行之前加上这行,请使用 “i” 命令,如下所示:
_sed '/--/ i "double dash after this line"' grephelp.txt_
**改变/变换**
如果需要改变/变换一行,则可以使用命令 “c”。
假设我们有个有一些私人信息的文档我们需要更改包含特定字符串的行。“c” 命令将改变整行,而不仅仅是搜索字符串。
假设我们想要阻止示例文件中包含单词 “PATTERN” 的每一行。更改的行将显示为 “This line is Top Secret”。命令是
_sed /PATTERN/ c This line is Top Secret grephelp.txt_
可以进行更改特定字母的大小写的转换。例如,我们可以使用命令 “y” 将所有小写 “a” 更改为大写 “A”如下所示
_sed y/a/A/ grephelp.txt_
可以指定多个字母,如 “abdg”如下命令所示
_sed y/abdg/ABDG/ grephelp.txt_
确保第二组字母与第一组字母的顺序相同,否则会被替换和转换。例如,字符串 y/a/D/ 将用大写 “D” 替换所有小写的 “a”。
**就地更改**
如果你确实要更改所使用的文件,请使用 “-i” 选项。
例如,要将 “PATTERN” 改为 “Pattern”并对文件进行更改则命令为
sed -i 's/PATTERN/Pattern/' grephelp.txt
现在文件 “grephelp.txt” 将被更改。“-i” 选项可以与上述任何命令一起使用来更改原始文件的内容。
练习这些命令并确保你理解它们。“sed” 命令非常强大。
--------------------------------------------------------------------------------
via: https://www.linux.org/threads/lfcs-sed-command.4561/
作者:[Jarret B ][a]
译者:[geekpi](https://github.com/geekpi)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:https://www.linux.org/threads/lfcs-sed-command.4561/