Introduction to SED
The SED stream editor (stream editor) is a simple text editing language that ranks the second in the Three Musketeers. SED does not directly process the source file, but instead reads the contents of the source file into memory (called pattern space), and then uses the SED command processing in the pattern space to print the contents of the pattern space to the standard output.
Sed can realize the function: adding and removing, changing, replacing, filtering, fetching rows.
sed text processing schematic diagram
The syntax of the SED command:
sed [options] ' addresscommand [modifier] ' inputfile (input file)
Explanation of the parameters of sed command syntax
Experimental environment
[[email protected] tmp]# cat userpasswd.txt 1 stu012 9cab83 stu024 28f375 stu036 cd4ef7 stu048 221ec9 stu0510 f7a98[[email protected] tmp]#
Options
-N: Cancels the default output, does not print (output) The contents of the mode space
Often used with p to print only rows that match the criteria (cancel the default output, but print rows that match the criteria)
[[email protected] tmp]# sed -n ‘s#stu01#stu#g‘p userpasswd.txt 1 stu[[email protected] tmp]#
-I: Modify the source file directly (use caution to avoid errors in the source file)
-E: Multiple operations at the same time
-r: Using extended expressions
Address: Range of addresses
1, linenumber: A specific line, specifying the first line, that is, n;$: indicates the last line
[[email protected] tmp]# sed -n 2p userpasswd.txt 2 9cab8[[email protected] tmp]#[[email protected] tmp]# sed -n ‘$p‘ userpasswd.txt 10 f7a98[[email protected] tmp]#
2, Start,end: Start line, end line, such as "2,5", from line 2nd to line 5th
[Email protected] tmp]# sed-n 2,5p userpasswd.txt
2 9cab83 stu024 28f375 stu03[[email protected] tmp]#
3, Mode1,mode2: Starting from the first line matched by pattern 1, to the end of the first line matched by pattern 2
[[email protected] tmp]# sed -n ‘/stu02/,6p‘ userpasswd.txt 3 stu024 28f375 stu036 cd4ef[[email protected] tmp]#
4, Startline,+n: Starting from the StartLine n rows, for example: "5,+2", starting from line 5th to the next 2 lines, that is, from line 5th to line 7th
[[email protected] tmp]# sed -n 5,+2p userpasswd.txt 5 stu036 cd4ef7 stu04[[email protected]bogon tmp]#
5./^root/: The address range can use regular expressions, but must be enclosed in symbols "//", meaning to find lines that start with root
[[email protected] tmp]# sed -n ‘/.*stu.*/p‘ userpasswd.txt 1 stu013 stu025 stu037 stu049 stu05[[email protected] tmp]#
command:sed Execute command
1, D: Delete qualifying rows
[[email protected] tmp]# sed 2,3d userpasswd.txt 1 stu014 28f375 stu036 cd4ef7 stu048 221ec9 stu0510 f7a98[[email protected] tmp]#
2. P: Print the Qualifying line (output)
[[email protected] tmp]# sed -n 3,4p userpasswd.txt 3 stu024 28f37[[email protected] tmp]#
3, ' A content ': Append new content after the specified line
[[email protected] tmp]# sed ‘2a 11 hello‘ userpasswd.txt 1 stu012 9cab811 hello3 stu024 28f37…
4. ' I content ': Append new content before the specified line
[[email protected] tmp]# sed ‘2i 11 hello‘ userpasswd.txt 1 stu0111 hello2 9cab83 stu024 28f37…
5. R file: Add the specified file contents to the line following the qualifying row
[[email protected] tmp]# cat test who[[email protected] tmp]# sed ‘2r /tmp/test‘ userpasswd.txt 1 stu012 9cab8who3 stu024 28f37…
6. W File: Saves the contents of a qualifying row as a specified file
[[email protected] tmp]# sed -n ‘4w /tmp/test1‘ userpasswd.txt [[email protected] tmp]# cat test14 28f37[[email protected] tmp]#
7, s/mode/character/: Find replacement, can also be written as s# mode # character #, [email protected] Mode @ character "s/What to look for/replace content"
Modifier:
G: Replace all (global substitution)
[[email protected] tmp]# sed ‘s#stu01#stu#g‘ userpasswd.txt 1 stu2 9cab83 stu02…[[email protected] tmp]# sed -n ‘s#stu01#stu#g‘p userpasswd.txt (只打印符合条件的行)1 stu[[email protected] tmp]#i:忽略字符的大小写[[email protected] tmp]# sed ‘s#sTu01#stu#g‘ userpasswd.txt 1 stu012 9cab83 stu02…[[email protected] tmp]# sed ‘s#sTu01#stu#gi‘ userpasswd.txt 1 stu2 9cab83 stu02…
8. L: Print invisible characters
[[email protected] tmp]# sed -n l userpasswd.txt 1 stu01$2 9cab8$3 stu02$4 28f37$5 stu03$… [[email protected] tmp]#
Special symbols:
&: Represents the content being replaced
[[email protected] tmp]# sed ‘s#t#--&--#g‘ userpasswd.txt 1 s--t--u012 9cab83 s--t--u024 28f375 s--t--u03…
Introduction to the SED of the Three Musketeers of Linux