sed is a good file processing tool, itself is a pipeline command, mainly in the behavior of the unit processing, you can replace the data rows, delete, add, select and other specific work.
Sed is essentially an editor, but it is non-interactive, which is different from vim, and it is a character stream oriented, and the input character stream is output after sed processing. These two features make SED a very useful processing tool under the command line.
sed parameter [-NEFR] action [function ] file options and Parameters: -N: Use Quiet (silent) mode. In the general sed -n parameter, only the line (or action) that is specially processed by SED is listed. -E: sed action edit; -F: Directly sed< /span> action; -R: sed -I: Directly modifies the contents of the read file, not the output to the terminal.
Action Description: [N1[,N2]]functionof ten,[action behavior] " functionsed -1, 20s/old/new/g!
* Usage Examples
Case 1: additions/deletions to behavioral units
1)/etc/passwd The contents of the list and print the line number, and delete the 2nd to 5th line.
[[email protected] ~]# passwd | sed 2,5d 1 root:x:0 : 0 : Root:/root:/bin/6 sync : X:5 : 0 : sync :/sbin:/bin/sync 7 shutdown:x:6 : 0 : shutdown:/ Sbin:/sbin/shutdown ..... (omitted later) .....
Sed's action is ' 2,5d ', and that D is delete because 2-5 lines
deleted, so there are no more than 2-5 rows of data displayed. Also note that the action after the SED,
must be enclosed in a ' two single quotation mark.
Case 2: Just delete line 2nd
nl /etc/passwdsed'2d'
Case 3: To delete 3rd to last row
nl /etc/passwdsed'3, $d'
Case 4: After the second line (i.e. the third line), add "drink tea?" Words!
[Email protected] ~]# NL/ETC/PASSWD | Sed ' 2a drink tea ' 1 root:x:0:0:root:/root:/bin/bash2 bin:x:1:1:bin:/bin:/sbin/nologindrink tea3 daemon:x:2:2:daemon:/ Sbin:/sbin/nologin ..... (omitted later) .....
Case 5: If it is to be before the second line
Case 6: If you want to add more than two lines, add two lines after the second line, such as "Drink tea or ..." and "Drink beer?"
[Email protected] ~]# NL/ETC/PASSWD | Sed ' 2a Drink tea or ......> Drink beer? ' 1 root:x:0:0:root:/root:/bin/bash2 bin:x:1:1:bin:/bin:/sbin/nologindrink tea or ... drink beer? 3 Daemon:x:2:2: Daemon:/sbin:/sbin/nologin ..... (omitted later) .....
Each row must be a backslash "\" To add a new line Oh! So, in the example above, we can see that there is a \ presence on the last side of the first line.
Replace and display with the behavior unit
Case 7: Replace the contents of the 2–5 line as "No 2-5 number"?
[Email protected] ~]# NL/ETC/PASSWD | Sed ' 2,5c No 2-5 number ' 1 Root:x:0:0:root:/root:/bin/bashno 2-5 number6 sync:x:5:0:sync:/sbin:/bin/sync ..... (omitted later) .....
In this way we are able to replace the entire line of data!
Case 8: List only 第5-7 lines within the/etc/passwd file
[Email protected] ~]# NL/ETC/PASSWD | Sed-n ' 5,7p ' 5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin6 sync:x:5:0:sync:/sbin:/bin/sync7 shutdown:x:6:0:shutdown:/ Sbin:/sbin/shutdown
It is possible to select certain line numbers within a file to display through the display function of the SED in the behavior unit.
Search and display of data
Case 9: Search for a line with the root keyword/etc/passwd
nl/etc/passwd | Sed '/root/p ' 1 root:x:0:0:root:/root:/bin/bash1 root:x:0:0:root:/root:/bin/bash2 daemon:x:1:1: Daemon:/usr/sbin:/bin/sh3 bin:x:2:2:bin:/bin:/bin/sh4 sys:x:3:3:sys:/dev:/bin/sh5
If Root is found, the matching rows are also output in addition to outputting all rows.
Case 10: When using-N, only the rows containing the template will be printed.
nl/etc/passwd | Sed-n '/root/p ' 1 root:x:0:0:root:/root:/bin/bash
Search for and delete data
Case 11: Delete/etc/passwd All rows containing root, other rows output
nl/etc/passwd | Sed '/root/d ' 2 daemon:x:1:1:daemon:/usr/sbin:/bin/sh3 bin:x:2:2:bin:/bin:/bin/sh .... The following ignores # The first line of the match root has been deleted
Search for data and execute commands
Case 12: Search for/etc/passwd, find the root row, execute a set of commands in the following curly braces, separate each command with a semicolon, here, replace Bash with Blueshell, and then output this line:
nl/etc/passwd | Sed-n '/root/{s/bash/blueshell/;p} '
1 Root:x:0:0:root:/root:/bin/blueshell
Case 13: If you only replace/etc/passwd's first bash keyword as Blueshell, exit
nl/etc/passwd | Sed-n '/bash/{s/bash/blueshell/;p; q} ' 1 Root:x:0:0:root:/root:/bin/blueshell
The last Q is the exit.
search for and replace data
Case 14: 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 '
Case 15: First observation of the original information, using/sbin/ifconfig query IP
[Email protected] ~]#/sbin/ifconfig eth0eth0 Link encap:ethernet HWaddr 00:90:cc:a6:34:84inet addr:192.168.1.100 Bcast : 192.168.1.255 mask:255.255.255.0inet6 addr:fe80::290:ccff:fea6:3484/64 scope:linkup broadcast RUNNING MULTICAST MTU : Metric:1 ..... (omitted below) .....
Case 16: Delete the previous part of IP
[Email protected] ~]#/sbin/ifconfig eth0 | grep ' inet addr ' | Sed ' s/^.*addr://g ' 192.168.1.100 bcast:192.168.1.255 mask:255.255.255.0
Case 17: Delete the part after IP
[Email protected] ~]#/sbin/ifconfig eth0 | grep ' inet addr ' | Sed ' s/^.*addr://g ' | Sed ' s/bcast.*$//g ' 192.168.1.100
Multi-point editing
Case 18: An sed command that removes data from the third line to the end of/etc/passwd and replaces bash with Blueshell
nl/etc/passwd | Sed-e ' 3, $d '-e ' s/bash/blueshell/' 1 root:x:0:0:root:/root:/bin/blueshell2 daemon:x:1:1:daemon:/usr/sbin :/bin/sh
-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.
Modify file contents directly (Dangerous action)
Case 19:sed can directly modify the contents of the file without using pipe commands or data flow redirection! However, because this action will be directly modified to the original file, so please do not take the system configuration to test! Let's use the downloaded Regular_express.txt file to test it out!
Use SED to end each line in the Regular_express.txt. Then replace it!
[Email protected] ~]# sed-i ' s/\.$/\!/g ' regular_express.txt
Case 20: Using sed to add "# This is a test" directly on the last line of the Regular_express.txt
[[email protected] ~]# sed-i ' $a # This is a test ' regular_express.txt
Since $ represents the last line, and A's action is new, the file is finally added "# This is a test"!
Sed "-i" option can directly modify the file content, this feature 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!
Linux security essentials: use of SED commands