from:http://blog.csdn.net/engledb/article/details/19623087 Six, common examples
1. P command
The command p is used to display the contents of the pattern space. By default, sed prints the input lines on the screen, and option-n is used to cancel the default print operation. When the Select-N and command p appear simultaneously, sed prints the selected content.
Example:
[Plain] view plaincopy
- (1) Sed '/my/p ' datafile
- #默认情况下, sed prints all input lines on the standard output. If a row matches the pattern, the my,p command will print the line again.
- (2) sed-n '/my/p ' datafile
- #选项-N Cancels the default print for SED, and the p command prints the line of the matching mode my.
2, D command
Command d is used to delete input rows. Sed first copies the input line from the file into the pattern space, then executes the SED command on the line, and finally displays the contents of the pattern space on the screen. If command d is emitted, the input lines in the current mode space are deleted and not displayed.
Example:
[Plain] view plaincopy
- (1) Sed ' $d ' datafile
- #删除最后一行, the rest is displayed.
- (2) Sed '/my/d ' datafile
- #删除包含my的行, the rest is displayed.
3, S command
Example:
[Plain] view plaincopy
- (1) Sed ' s/^my/you/g ' datafile
- #命令末端的g表示在行内进行全局替换, that is, if a row appears with multiple my, all my my is replaced with you.
- (2) sed-n ' 1,20S/MY$/YOU/GP ' datafile
- #取消默认输出, Process 1 to 20 lines matching the line ending with my, replace all of my in-line I with you and print to the screen.
- (3) Sed ' s#my#your#g ' datafile
- #紧跟在s命令后的字符就是查找串和替换串之间的分隔符. Separating defaults is considered a forward slash, but can be changed. No matter what character (except newline, backslash), as soon as the S command is followed, a new string delimiter is formed.
4. E option
-E is an edit command that is used in cases where SED performs multiple editing tasks. All edit actions are applied to the line in the pattern buffer before the next line begins editing.
Example:
[Plain] view plaincopy
- Sed-e ' 1,10d '-e ' s/my/your/g ' datafile
- #选项-E is used for multiple edits. The first edit deletes the 第1-3 line. The second editor replaces all occurrences of my with your. Because these two edits are performed line by row (that is, both commands are executed on the current line in the pattern space), the order in which the commands are edited affects the result.
5. R command
The r command is a read command. SED uses this command to add the contents of a text file to a specific location in the current file.
For example:
[Plain] view plaincopy
- Sed '/my/r introduce.txt ' datafile
- #如果在文件datafile的某一行匹配到模式My, the contents of the file Introduce.txt are read after the line. If there is more than one row of my rows, the contents of the Introduce.txt file are read after the lines of my are present.
6, W command
Example:
[Plain] view plaincopy
- Sed-n '/hrwang/w me.txt ' datafile
7. A\ command
The A\ command is an append command, appended with the addition of new text to the current line in the file (that is, the line in the read-in mode buffer). The appended line of text is placed below the SED command. If more than one row is to be appended, each row must end with a backslash, except for the last line. The last line ends with a quotation mark and a file name.
Example:
[Plain] view plaincopy
- Sed '/^hrwang/a\
- >hrwang and Mjfan are husband\
- >and Wife ' DataFile
- #如果在datafile文件中发现匹配以hrwang开头的行, append Hrwang and Mjfan are husband and wife below the line
8. I\ command
The I\ command inserts a new text in front of the current line.
9. c\ command
SED uses this command to modify an existing text to a new text.
10, N command
SED uses this command to get the next line of input files and read them into the pattern buffer, and any sed command will be applied to the next line immediately following the matching line.
For example:
[Plain] view plaincopy
- Sed '/hrwang/{n;s/my/your/;} ' datafile
Note: If you need to use multiple commands, or if you need to nest addresses in an address range, you must enclose the command in curly braces, write only one command per line, or use semicolons to split multiple commands in the same row.
11, y command
This command is similar to the TR command in Unix/linux, where characters are converted from left to right in a one-to-one fashion. For example, y/abc/abc/will convert all lowercase a to a, lowercase B to B, and lowercase C to c.
For example:
[Plain] view plaincopy
- Sed ' 1,20y/hrwang12/hrwang^$/' datafile
- #将1到20行内, convert all lowercase hrwang to uppercase, convert 1 to ^, convert 2 to $.
- #正则表达式元字符对y命令不起作用. As with the S command delimiter, the slash can be replaced with other characters.
12. Q Command
The Q command will cause the SED program to exit without further processing.
[Plain] view plaincopy
- Sed '/hrwang/{s/hrwang/hrwang/;q;} ' datafile
13, h command and G command
To better illustrate these two commands, we first create the following text file:
[Plain] view plaincopy
- #cat datafile
- My name is Hrwang.
- Your name is Mjfan.
- Hrwang is Mjfan ' s husband.
- Mjfan is Hrwang ' s wife.
[Plain] view plaincopy
- Sed-e '/hrwang/h '-e ' $G ' datafile
- Sed-e '/hrwang/h '-e ' $G ' datafile
- #通过上面两条命令, you will find that H clears the contents of the original staging buffer, saving only the schema space that was saved in the last time the H was executed. The h command appends the rows of each match Hrwnag to the staging buffer.
- Sed-e '/hrwang/h '-e ' $g ' datafile
- Sed-e '/hrwang/h '-e ' $G ' datafile
- #通过上面两条命令, you'll find that G replaces the contents of the current row in the schema space with the content of the staging buffer, replacing the last row. The G command appends the contents of the staging buffer to the current line of the schema space. This is appended to the end.
Shell Script sed command