Sed-stream Editor for filtering and transforming text
Filter and convert text for the stream editor
sed [-nerf] [action]
Parameters:
-I modify source file danger
-e perform SED action edits directly in command line mode
-F writes the SED action directly in a file, and-f filename performs the SED action within the filename
-R: Using extended regular expressions
-n silent mode, all data from stdin in the default sed is generally listed on the screen, but if you add-N, only the line that has been specially processed by SED will be listed.
sed " /^a/p " sed . txt //The line starting with "a" will be printed, you can see that B is also printed out, and a is printed two times, once the SED is the default is to print the contents of the pattern space, the second time is the P parameter printed again sed " /^a/p " sed . txt // Use the-n parameter to print only the matching content
Action Description: [N1,N2] function
1.LineNumber
Rows that match exactly
2.StarLine, +n
From Startline onwards, to the end of the next n line, it is n+1 line
3.startline,endline
$ represents the last line
[Email protected] tmp]#sed-N"1,2p"/etc/passwd//print 1-2 lines of contentroot:x:0:0: root:/root:/bin/bashbin:x:1:1: bin:/bin:/sbin/Nologin[[email protected] tmp]#sed-N"30,\ $p"/etc/passwd //Promise 30 to the last line of content mysql:x:502:502::/home/mysql:/sbin/nologinapache:x: -: -: apache:/var/www:/sbin/Nologin[[email protected] tmp]#sed-N"102"/etc/passwd //Print the contents of line 30th mysql:x:502:502::/home/mysql:/sbin/nologin
4./matching mode/support regular expression
such as:/^root/
5./partern/,/partern/
The first line that is matched by the partern1 begins at the end of the line that is first matched to the partern2, and all the rows in the middle
[Email protected] tmp]#sed-N"/bash$/p"/etc/passwd//print lines ending in bashroot:x:0:0: root:/root:/bin/bashalong:x:501: -::/home/along:/bin/Bash[[email protected] tmp]#sed-N"/^rpc/p"/etc/passwd//Print line rpc:x starting with RPC: +: +: Rpcbind daemon:/var/cache/rpcbind:/sbin/nologinrpcuser:x: in: in: RPC Service user:/var/lib/nfs:/sbin/Nologin[[email protected] tmp]#sed-N"/^\ (. *\) root/p"/etc/passwd//printing starts with any character and has a root line in the middleroot:x:0:0: root:/root:/bin/bashoperator:x: One:0: operator:/root:/sbin/Nologin[[email protected] tmp]#
function
D: Delete rows that match the criteria
P: Print rows that match the criteria
With this command, the specified content is printed two times, because the SED command defaults to printing the contents of the pattern space, and P is the print, so it repeats
A \string: Appends a new line to the specified line, with the contents of a string
The meaning of a append append, that is, append the content after the specified line
[[email protected] tmp]# sed .txt ab[[email protected] tmp]# a = c "//set a variable a [[email protected] tmp]# " 2a $a " sed .txt//appends the value of variable A to the second line abc[[email protected] tmp]# sed 2a \ $a " sed .txt//special symbols need to be escaped ab$a[[email Protected] tmp]#
I \string: Adds a new row before the specified line, the content is string
"I" is the meaning of insert.
Cat sed sed'1i 1'sed . txt//Add a row to the front of the specified line 1
R file: Adds the contents of the specified file to the qualifying line
Cat sed catsed"2r./sed1.txt"sed.txt
W file: Adds the contents of the specified range to the specified file
sed " 30,\ $w./a.txt " /etc/passwd //writes the last two lines of the passwd file to the A.txt file go to cat a.txt mysql:x: 502:502::/home/mysql:/sbin/nologinapache:x:$:apache:/var/ www:/sbin/
s/partern/string/modifier Lookup replaces the default only to replace the first string that is matched to
Modifier:
G: Global Substitution
I: Ignore case
Can not only use S///S # # # s @@@
The S command can also support a back reference
& can be used as a replacement, but not for all situations
Replace "L" with "L"
sed commands in Linux