Sed
SED is a powerful text-processing tool
You can use regular matching, insert and delete text, and so on.
When SED is processed, one row at a time, each time the current processing is stored in a temporary buffer, the output buffer content to the screen after processing, and then read the next line into the buffer, so repeat, until the end.
1. command format and Parameters
sed [-NEFR] [action] File
Parameters:
-N Quiet mode, when the SED processing, all data from stdin will be output to the terminal, plus-n will only output which line of processing
-E performs SED action editing directly on the command column
-F Write the SED action directly in the file
-R SED action supports extended regular expression (default is only the underlying regular)
-I directly modify the contents of the file (use caution, especially when doing exercises with system files)
Action:
AAppend: Increment, increment on the next line of the current row
C : Replace, replace N1 to N2 line
D Delete: Remove
I Insert, the previous line of the current row is inserted
P printing, often with-n use
S Replace, s/old/new/g
2, the basic usage detailed
(1) Add a line after the first line
[[email protected] ~]# NL file.txt | SED "1a Add text" 1 wtmp begins Mon Feb 14:26:08 2014add text 2 192.168.0.1 3 162.12.0.123< C12/>4 the last line
(2) Add a row before the first line
[[email protected] ~]# NL file.txt | Sed "1i Add text" Add text 1 wtmp begins Mon Feb 14:26:08 2 192.168.0.1 3 162.12.0.123
4 the last line
(3) Delete 2nd, 3 lines
[[email protected] ~]# NL file.txt | Sed "2,3d" 1 wtmp begins Mon Feb 14:26:08 4 This was the last line
(4) print 2nd, 3 lines
[Email protected] ~]# sed-n "2,3p" file.txt 192.168.0.1162.12.0.123
The point here is to try to use-N, otherwise the result will appear
[[Email protected] ~]# sed "2,3p" file.txt wtmp begins Mon Feb 24 14:26:08 2014192.168.0.1192.168.0.1162.12.0.123162.12.0. 123this is the last line
(5) Change 168 to 169
First look at the source file
[Email protected]lhost ~]# cat file.txt wtmp begins Mon Feb 14:26:08 2014192.168.0.1162.12.0.123this
After processing
[Email protected] ~]# sed "s/168/169/g" file.txt wtmp begins Mon Feb 14:26:08 2014192.169.0.1162.12.0.123this is the L AST Line
(6) Inserting multiple lines
[[email protected] ~]# NL file.txt | Sed "2afirst\nsecond" file.txt wtmp begins Mon Feb 14:26:08 2014192.168.0.1firstsecond162.12.0.123this
(7) match the data and do the operation
Just add a regular match on the basis above
Sed "/matching mode/method of processing" file.txt
Sed "/^root/d" file.txt to start with root delete
For example
Match begin, and delete a row
[[email protected] ~]# NL file.txt | Sed "/begin/d" 2 192.168.0.1 3 162.12.0.123 4 This was the last line
Match 123, and replace the 123 line 162 with 172
[[email protected] ~]# NL file.txt | Sed "/123/{s/162/172/g;q}" 1 wtmp begins Mon Feb 14:26:08 2 192.168.0.1 3 172.12.0.123 4 The last line
Here the curly braces {} Can execute multiple commands, separated, Q is exited
(8) Continuous editing-e
Delete the second row, and the match replaces the last with the new
<pre name= "code" class= "plain" >[[email protected] ~]# nl file.txt | Sed-e "2d"-E "s/last/new/" 1 wtmp begins Mon Feb 14:26:08 3 162.12.0.123 4 this I s the new Line
(9) directly modify the file, remember not to modify the system files
[Email protected] ~]# sed-i "/begin/{s/24/25/g}" file.txt [[email protected] ~]# cat file.txt wtmp begins Mon Feb 25 14: 26:08 2014192.168.0.1162.12.0.123this is the last line
a city a more interesting example
How to replace \ n that is to put all the rows in a row
The first way
[[Email protected] ~]# sed ': A; n;$!ba;s/\n//g ' file.txt wtmp begins Mon Feb-14:26:08 192.168.0.1 162.12.0.123
The second way
[[Email protected] ~]# TR "\ n" "" < File.txt wtmp begins Mon Feb 14:26:08 "192.168.0.1 162.12.0.123 This is the Last Linen
Linux sed command details + How to replace line break "\ n" (many interviews ask)