SED is a non-interactive text editor that can edit text files and standard input
Sed simply edits a copy of the original file in the buffer and does not edit the original file
Three ways in which SED commands are invoked
1. Call sed on shell command line output command
sed [option] ' sed cmd ' file
2. Insert the SED command into the script
sed [option]-f ' sed.sh ' file
#vi sed.sh sed The script to invoke is sed.sh
#/file:/a\this is a test line!!!
Invoke script
#sed-F sed.sh Test
3. Insert the SED command into the script and execute it directly
./sed.sh file
#vi sed.sh sed Script
#!/bin/sed-f
#/file:/a\this is a test line!!!
#./sed.sh Test Execution Script
sed command options and Implications
-N #不打印所有行到标准输出
-e #表示将下一个字符串解析为sed编辑命令 If only one edit command is passed to the SED,-E option can be omitted
-F #调用sed脚本文件
How to position text with sed command
X #x为指定行号
X, y #指定从x到y的行号范围
/pattern/#查询包含模式的行
/pattern/pattern/#查询包含两个模式的行
/pattern/,x #从与pattern的匹配行到x号行之间的行
x,/pattern/#从x号行到与pattern的匹配行之间的行
x,y! #查询不包括x和y行号的行
sed Edit Command
P #打印匹配行
= #打印文件行号
A\ #在定位行号之后追加文本信息
I\ #在定位行号之前插入文本信息
D #删除定位行
C\ #用新文本替换定位文本
S #使用替换模式替换相应模式
R #从另一个文件中读文本
W #将文本写入到一个文件
Y #变换字符
Q #第一个模式匹配完成后退出
L #显示与八进制ASCII码等价的控制字符
{} #在定位行执行的命令组
N #读取下一个输入行, new rows are processed with the next command
H #将模式缓冲区的文本复制到保持缓冲区
H #将模式缓冲区的文本追加到保持缓冲区
X #互换模式缓冲区和保持缓冲区的内容
G #将保持缓冲区的内容复制到模式缓冲区
G #将保持缓冲区的内容追加到模式缓冲区
======================================================================================
sed example
Print the first line
#sed-n ' 1p ' test
Print all content, and have two first row
#sed ' 1p ' test
Print 3-6 lines of text
#sed-n ' 3,6p ' test
Pattern matching, print out the row containing the certificate
#sed-n '/certificate/p ' test
sed [-e]: means that the next string is resolved to the SED edit command
(1), print the content of certificate matching mode
(2), print certificate matching mode line number
(3), print certificate match mode content and line number
#sed-n '/certificate/p ' test
#sed-n '/certificate/= ' test
#sed-N-E '/certificate/p '-e '/certificate/= ' test
The general format for SED with multiple edit commands is
#sed [option]-e cmd1-e cmd2-e cmdn inputfile
In the text string file: Append the following: This is a test file!!!
#sed '/file:/a\this is a test file!!! ' Test
Append the Wanglei user directly to the root of the/etc/passwd text
#sed-i '/root:x:0:0/a\wanglei:x:0:0:wanglei:/wanglei:/bin/bash '/etc/passwd
Prints the line containing the. character in the text test
#sed-n '/\./p ' test
Prints the line containing the $ character in the text test
#sed-n '/\$/p ' test
Print out the last line in the text test
#sed-n ' $p ' test
Print a line with a bus ending string
#sed-n '/.*bus$/p ' test
Print a line that is not between 2-10
#sed-n ' 2,10!p ' test
Print out all lines except string certificate
#sed-n '/certificate/!p ' test
Insert the contents of the line in the text test above the front of the string "This is a test lines!!!" and saved in the file
#sed-I '/above/i\ @This is a test line!!! ' Test
Prints the contents of the above string between 13 lines in the text test
#sed-n '/above/,13p ' test
Prints the line in the text test 3 lines to the string/home/globus/.
#sed-n ' 3,/\/home\/globus\/\./p ' test
Insert a row before the test text $88 string "This is a test \i line!!!"
#sed '/\$88/i\this is a test \\i line!!! ' Test
Replaces the text line of a matched string with a new text in \c
#sed '/Specify address/c\test ' file
Replace lines in text that match above strings with new lines "This is a new line!!!"
#sed '/above/c\this is a new line!!! ' Test
Replace the 1,5,13 line in the text with "This is a 1 lines!!!" (1,5,13)
#sed-E ' 1c\this is a 1 line!!! '-e ' 5c\this is a 5 line!!! '-e ' 13c\this is a line!!! ' Test
Delete Line 3rd in the test text
#sed ' 3d ' test
Delete 1-3 lines from the test text
#sed ' 1,3d ' test
Delete the contents of the string above to 13 rows in the test text
#sed '/above/,13d ' test
Delete the last line of text
#sed ' $d ' test
Delete 5 lines to the last row
#sed ' 5, $d ' test
Delete a case-insensitive string in text certificate line
#sed '/[cc][ee][rr][tt][ii][ff][ii][cc][aa][tt][ee]/d ' test
Replace text #替换一个或多个字符串
s/replaced by string/new string/[replace option]
G #表示替换文本中所有出现被替换字符串之处
P #与-N option in combination to print only replacement lines
W File name #表示将输出定向到一个文件
#sed-n ' s/replaced string/new string/p ' input file
Replace the string certificate in the test file with certificate (multiple in the same row replaces only the previous one)
#sed-n ' s/certificate/certificate/p ' test
Replace all string above in the test file with Shabi and save in/root/char file
#sed-n ' s/above/shabi/pg w/root/char ' test
Replace the string in the test text, replace above with Char, and replace the second char that appears in each line of the article
#sed-n ' s/above/char/2p ' test
Replace the string in the test text, replace above with CharSet, replace the 6th occurrence of each line in the text above, and save to/root/save
#sed-n ' s/above/charset/6p w/root/save ' xx
Replace the string certificate in the test text with the Oye and save as Oye
#sed-N ' s/certificate/oye/pg w Oye ' test
& can be used to save the replaced string for invocation of the,& symbol equivalent to the replacement string
#sed-N ' s/seu/(&)/PG ' Test
#sed-n ' s/seu/(seu)/pg ' test
Prints 1-5 rows of the test file and writes to the output
#sed-n ' 1,5 w output ' test
Reads the contents of the file xx into the file test file after the string above
#sed '/above/r xx ' test
Reads the contents of the file xx into the file test file after line 5th
#sed ' 5r xx ' test
Reads the contents of the file xx into the file test file after the 第5-7 line
#sed ' 5,7r xx ' test
Reads the contents of the file xx into the file test file after line 5,7,12
#sed-E ' 5r xx '-e ' 7r xx '-e ' 12r xx ' test
Print out the. r.* matching file in the test file
#sed-n '/.r.*/p ' test
Prints the. r.* matching file in the test file and prints only the first line that matches
#sed '/.r.*/q ' test
==========================================================
Transform command y
Represents a character transformation, transforms a series of characters into corresponding characters, processes characters one by one, 1 handles 1 2 processing 2 ...
#sed ' y/transformed character sequence/transformed character sequence ' input file
Transform all A, B, C, D, E, F, G, H, I in the test file into numbers 1, 2, 3, 4, 5, 6, 7, 8, 9
#sed ' y/abcdefghi/123456789/' test
Converts a, B, O, V, E, characters in the test file to a, B, O, V, E
#sed ' y/above/above/' test
Replace the 27 English letters in the test file with 1234567890-= ' [email protected]#$%^&* () _+/
#sed ' y/abcdefghijklmnopqrstuvwxyz/1234567890-= ' [email protected]#$%^&* () _+/' test
Execute command group {} on anchor row
#sed-n '/certificate/{p;=} ' test
Equivalent to: sed-n-e '/certificate/p '-e '/certificate/= ' test
The line that matches the Certificate keyword in the test text replaces all I with I, replacing the first Le with a 99
#sed '/certificate/{s/i/i/g;s/le/99/;} ' test
N #处理匹配行的下一行
Replace the string in the test text with the user character of the following line certificate 99
#sed '/certificate/{n;s/user/99/;} ' test
Replace the string in the test text with the \ character of the following line certificate gang
#sed '/certificate/{n;s/\\/gang/;} ' test
=================================
sed buffer processing
#sed-E '/file1/h '-e '/file2/x '-e ' $G ' xxx
1, H #将匹配file1的行复制到保持缓冲区
2, x #将匹配file2的行和保持缓冲区的行互换 (File1 line)
3, G #将保持缓冲区的内容追加到模式缓冲区 $ for last line
Use semicolons to separate multiple edit commands-e {};
Prints the line and line number of the string ceritificate in the test file using semicolons
#sed-n '/certificate/p;/certificate/= ' test
Replace the file1 characters in the xxx file with WL, match file2 with FX, match File3 to YWC, match file4 to Ly
#sed ' s/file1/wl/; s/file2/fx/; s/file3/ywc/; s/file4/ly/; ' xxx
The following script is useful to the previous instance
===================================================================== =
1---This is a read FILE1
2---The is a read File2
3---The is a read FILE3
4---The is a read FILE4
5---T A read FILE5
6---This is a read FILE6
7---The is a read File7
=========================================== ===========================
1\this is a Certificate Request file:
2\
3\it should being meailed to [email Protected]
4\
5\===========================================================
6\certificate Subject:
7\
8\ /o=grid/ou=globustest/ou=simpleca-seugridl.seu.edu.cn/ou=seu.edu.cn/cn=globus
9\
10\ The above string is known as your user certificate subject, and it uniquely identifies
11\this user. $88
12\to INS Tall This user certificate and save this e-mail message into the following file.
13\
14\/home/globus/.globus/usercert.pem
15\fawnbusgfqa
16\fwafawfbus
Shell Programming---sed command detailed