In the study and use of Linux, the importance of sed,awk,grep usage is self-evident, this article summarizes the usage of SED from the following aspects:
1, the meaning of sed
The SED is the stream editor, and that is the flow editors, and the vi we use is different, and VI is the full screen editor. and SED is to row the contents of the file into memory, in the SED to specify the command to process, and then output to the screen.
2. Use and format of SED
sed [options] ' Startadd,endaddcommand ' files,...
[Options] refers to the option of SED. Can omit
Startadd,endadd refers to the representation of addresses in SED
Command refers to the processing of SED commands.
Files,.. Can be multiple, which is the processing object of the SED. Put that file in memory and then line it up.
3. How the address is represented
' 1,3p ' passwd #表示的含义是, print passwd files 1 through the third line. Absolute line number notation
[Email protected] testdir]# sed-n ' 1,3p ' passwd
Root:x:0:0:root:/root:/bin/bash
Bin:x:1:1:bin:/bin:/sbin/nologin
Daemon:x:2:2:daemon:/sbin:/sbin/nologin
' 1,+3p ' passwd #表示的含义是: Prints the contents of the first line and the following three lines. Relative line number notation
[Email protected] testdir]# sed-n ' 1,+3p ' passwd
Root:x:0:0:root:/root:/bin/bash
Bin:x:1:1:bin:/bin:/sbin/nologin
Daemon:x:2:2:daemon:/sbin:/sbin/nologin
Adm:x:3:4:adm:/var/adm:/sbin/nologin
' $p ' passwd #表示打印从38行到最后一行的passwd的内容 $ represents the last line
[Email protected] testdir]# sed-n ', $p ' passwd
User1:x:504:504::/home/user1:/bin/bash
User2:x:505:505::/home/user2:/bin/bash
User3:x:506:506::/home/user3:/bin/bash
'/pattern/command ' #表示匹配到patern的行打印, pattern can be a regular expression
[Email protected] testdir]# sed-n '/^root/p ' passwd
Root:x:0:0:root:/root:/bin/bash
Rootbisu:x:500:500:centos_x641:/home/rootbisu:/bin/bash
'/pattern1/,/pattern2/command ' uses the Command to handle the/pattern1/first match to the row, to/pattern2/the first match to the contents of the line
[Email protected] testdir]# sed-n '/^\<root\>/,/^\<bin\>/p ' passwd
Root:x:0:0:root:/root:/bin/bash
Bin:x:1:1:bin:/bin:/sbin/nologin
sed ' 3p ' passwd # indicates the third line of print passwd
[Email protected] testdir]# sed-n ' 3p ' passwd
Daemon:x:2:2:daemon:/sbin:/sbin/nologin
4. Use of commands and parameters in SED
Since SED is a stream editor, his main command function should be on the editor. Common editing operations include, print, delete, replace, add, and so on.
d: To delete rows that match the criteria
P: Indicates that the line that matches the criteria is printed
A: After qualifying the line , format: a\string append new Content A is append # A after what the output is. [[Email protected] testdir]# sed '/^\<root\>/a\appendtest ' passwd
Root:x:0:0:root:/root:/bin/bash
Appendtest
I:insert, the table inserts a new string before qualifying rows. Format i\string
[[Email protected] testdir]# sed '/^\<root\>/i\appendtest ' passwd
Appendtest
Root:x:0:0:root:/root:/bin/bash
R:read, Rfile indicates that the contents of the read file are appended after the qualifying row
[[Email protected] testdir]# sed '/^\<root\>/rgrep.txt ' passwd
Root:x:0:0:root:/root:/bin/bash
This is root
The user is Mroot
Rooter is a dog ' s name.
Mrooter is not a word
Bin:x:1:1:bin:/bin:/sbin/nologin
W:write,wfile indicates that the qualifying line is written to file
[Email protected] testdir]# sed-n '/^\<root\>/wgrep.txt ' passwd
[email protected] testdir]# cat Grep.txt
Root:x:0:0:root:/root:/bin/bash
s means find and replace # Replace the first match to the item
[[Email protected] testdir]# sed ' s/root/root/' passwd
Root:x:0:0:root:/root:/bin/bash
# plus the parameter G means replace all matches to the item
[[Email protected] testdir]# sed ' s/root/root/g ' passwd
Root:x:0:0:root:/root:/bin/bash
5, the commonly used parameters:
-N: Indicates silent mode, output only processed rows, other outputs
-I: Will effect the command result on the processed file
-R: Indicates that extended regular expressions can be used in pattern
-E: Indicates that multiple commands can be used. Sed-e ' 10,11p '-e ' 12,13d '
6, the summary of the use of SED and matters needing attention
The use of SED is critical, and it is important to remember that the use and format of the SED, the representation of the address, and the commands used, as well as the parameters of the command, and the details that need to be noted here are the latter references that can be used in sed.
There is a text content as follows:
He like he
She love hers love
He like his love
She love he like
There is a need, want to turn like into Liker, want to turn love into lover should be how to deal with? This time you can use the latter reference.
[Email protected] testdir]# sed ' s/\ (L. e\)/\1r/g ' test
He liker his liker
She lover her lover
He liker his lover
She lover her liker
[Email protected] testdir]# sed ' s/\ (L. e\)/\ (l,,e\) r/g ' test
He (L,,e) r his (l,,e) r
She (L,,e) r Her (l,,e) r
He (L,,e) r his (l,,e) r
She (L,,e) r Her (l,,e) r
Note that we find that ' s/string1/string2/g ' where string can be a regular expression, and if the-r option is used, you can also use an extended regular expression, but string2 cannot use regular expressions.
Summary of usage of SED