In the previous section we have learned grep and today we are going to learn about sed. It is processed according to script instructions, editing one or more text files, simplifying the repeated operation of the file, writing the conversion program and so on.
SED usage
SED option Action input file
parameter Description :
-N does not output to the screen, does not print (default printing)
-e Multi-point edit input text file
-F to process the input text file with the script file specified in the option
-R Extended Regular expression
-H Display Help
-V Display version information
The "-i" option of-i sed can directly modify the file contents, which is very helpful! For example, if you have a 1 million-line file that you want to add some text to in line 100th, using VIM at this point may go insane! Because the file is too big! What do you do? Just use sed! You don't even need to use VIM to revise the features directly modified/replaced by SED!
Instance:
- N :
[[Email protected] ~] # seq 1 |sed-n ' 8p '8[root@localhost ~]# seq 1 |sed-n "$[$ (seq 1 10|wc-l) -1]p"9 # output Countdown second line
- e:
[[Email protected] ~] # sed-e 2a\desk eHow is it? Apple Banana appledeskwine tea juicedesk Chair
- R:
[[email protected] app] # cat/etc/passwd |sed-r ' s/(R. t). *\1/hello/g 'hello:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin :/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/ Nologinsync:x:5:0:sync:/sbin:/bin/syncshutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
Action Description :
A is added, a can be followed by a string, and these strings appear on a new line (the current next line) ~
c instead of replacing the specified line contents
d Delete delete the specified line
I insert a string after I can be inserted, and these strings will appear on a new line (the current line);
P Printing is the printing of a selected data. Normally p will run with the parameter Sed-n ~
s replaces work that can be replaced directly! Usually this s action can be paired with formal notation!
= Print line number
NL Explicit Line number
1. Additions/deletions to behavioral units
Copy the contents of the/etc/passwd to the/app directory, listing and printing the line number.
-A after the second line (that is, add the third line) and add drink tea words!
[[email protected] app] # nl/etc/passwd | sed ' 2a drink tea ' 1 root:x:0:0:root:/root:/bin/bash 2 bin:x:1:1:bin:/bin:/sbin/nologindrink Tea 3 daemon:x:2:2:daemon:/sbin:/sbin/nologin 4 adm:x:3:4:adm:/var/adm:/sbin/ Nologin 5 Lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
-I if it's going to be in front of the second line
[[email protected] app] # nl/etc/passwd | sed ' 2i drink tea ' 1 root:x:0:0:root:/root:/bin/bashdrink tea 2 bin:x:1:1:bin:/bin:/sbin/nologin 3 Daemon:x:2:2:daemon:/sbin:/sbin/nologin
- D Line 2nd to 5th Delete!
[[email protected] app] # nl/etc/passwd |sed ' 2,5d ' 1 root:x:0:0:root:/root:/bin/bash 6 sync:x:5:0:sync:/sbin:/bin/sync 7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown 8 halt:x:7:0:halt:/sbin:/sbin/Halt 9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:o perator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin
Sed's action is ' 2,5d ', and that D is delete! Because 2-5 rows to him deleted, so the data displayed there is no 2-5 line Luo ~ In addition, note that the original should be issued SED-E only, no-e also line! It is also important to note that the action behind the SED must be enclosed in "two single quotes."
2. Substitution and display of behavior units
- C replaces the contents of the 2–5 line as "No 2-5 number"?
[[email protected] app] # nl/etc/passwd | sed ' 2,5c No 2-5 number ' 1 root:x:0:0:root:/root:/bin/ 2-5 number 6 sync:x:5:0:sync:/sbin:/bin/Sync 7 Shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
3. Search and display of data
Search for a line with the root keyword/etc/passwd
[[email protected] app] # nl/etc/passwd | sed-n '/root/p ' 1 root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin
4. Search for and replace data
In addition to the entire line of processing mode, SED can also be used to search and replace part of the data in the behavioral units. Basically, the search for SED is similar to the VI equivalent to the alternative! He's kind of like this:
Sed ' s/to be substituted string/new string/g '
[[email protected] app] # ifconfig ens33|sed-n ' 2p ' |sed ' s/^.*inet//g ' |sed ' s/net.*$//g '
5. Multi-point editing
An sed command that removes data from the third line to the end of/etc/passwd and replaces bash with Blueshell. -E for multi-point editing, the first edit command deletes the data from the third line to the end of the/etc/passwd, and the second command searches for bash instead of Blueshell
[[email protected] app] # nl/etc/passwd | sed-e ' 3, $d '-e ' s/bash/blueshell/' 1 root:x:0:0:root:/root:/bin/blueshell 2 bin:x:1:1:bin:/bin:/sbin/nologin
6. address delimitation:
(1) Not to address: The full text of the processing
(2) Single address:
#: Specified line, $: last line
/pattern/: Each row to which the pattern can be matched
(3) Address range:
#,#
#,+#
/pat1/,/pat2/
#,/pat1/
(4) ~: Step forward
Odd lines
2~2 even rows
Advanced Usage :
H-mode space coverage to hold space
H-mode space is appended to the hold space
G keep space covered to pattern space
G hold space append to mode space
x keep space and mode space content interchangeable
The next line of n rows is overwritten to the pattern space
The next line of N rows is appended to the mode space
D keep the last line
D Delete Pattern space rows
Example:
(1) Output even line
[[Email protected] ~] # 2468
(2) Show only odd lines
[[Email protected] ~] # 13579
(3) Reverse output
[[Email protected] ~] # seq 1 |sed-n ' 1! G;h, $p '10987654321
(4) Output countdown after two lines
[[Email protected] ~] # seq 1 |sed ' $! n;$! D '910
(5) Empty rows are deleted, followed by a blank line after each line, which guarantees that there is only one blank line after each row
[[Email protected] ~] # 123456
Linux Text processing sed of the Three Musketeers