SED command usage parameters
[[Email protected] ~]# sed [-NEFR] [action]
Options and Parameters:
-N: Use Quiet (silent) mode. In the usage of general sed, all data from STDIN is generally listed on the terminal. However, if you add the-n parameter, only the line (or action) that is specially processed by SED is listed.
-E: Action editing of SED directly in command-line mode;
-F: The action of SED is written directly in a file, and-f filename can run the SED action within filename;
-r:sed's actions support the syntax of extended formal notation. (The default is the basic formal French notation)
-I: Directly modifies the contents of the read file, not the output to the terminal.
Action Description: [n1[,n2]]function
N1, N2: Not necessarily exist, generally represents "select the number of lines of action", for example, if my action is required between 10 to 20 rows, then "10,20[Action Behavior"
function
A: New, a can be followed by a string, and these strings will appear in a new line (the current next line) ~
C: Replace, C can be followed by strings, these strings can replace the line between N1,N2!
D: Delete, because it is deleted ah, so d usually do not pick up any boom;
I: Insert, I can be followed by the string, and these strings will appear on a new line (the current line);
P: Print, that is, print out a selected data. Normally p will run with the parameter Sed-n ~
S: Replace, can be directly replaced by work! Usually this s action can be paired with formal notation! For example 1,20s/old/new/g is!
Copy Code
Add/Remove as behavior unit
List the contents of the/etc/passwd and print the line number, and at the same time, delete the 2nd to 5th line!
[Email protected] ~]# 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
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! Also note that the action behind the SED, be sure to enclose in ' two single quotes!
Just delete line 2nd
nl/etc/passwd | Sed ' 2d '
To delete 3rd to last row
nl/etc/passwd | Sed ' 3, $d '
Add "drink Tea" after the second line (i.e. the third line) Words!
[Email protected] ~]# NL/ETC/PASSWD | Sed ' 2a drink tea '
1 Root:x:0:0:root:/root:/bin/bash
2 Bin:x:1:1:bin:/bin:/sbin/nologin
Drink tea
3 Daemon:x:2:2:daemon:/sbin:/sbin/nologin
..... (omitted later) .....
So if it's going to be before the second line,
nl/etc/passwd | Sed ' 2i drink tea '
If you want to add more than two lines, add two lines after the second line, such as "Drink tea or ..." and "Drink beer?"
Copy Code
[Email protected] ~]# NL/ETC/PASSWD | Sed ' 2a Drink tea or ... \
> Drink beer? '
1 Root:x:0:0:root:/root:/bin/bash
2 Bin:x:1:1:bin:/bin:/sbin/nologin
Drink tea or ...
Drink beer?
3 Daemon:x:2:2:daemon:/sbin:/sbin/nologin
..... (omitted later) .....
Copy Code
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
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/bash
No 2-5 Number
6 Sync:x:5:0:sync:/sbin:/bin/sync
..... (omitted later) .....
In this way we are able to replace the entire line of data!
List only 第5-7 lines within a/etc/passwd file
[Email protected] ~]# NL/ETC/PASSWD | Sed-n ' 5,7p '
5 Lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
6 Sync:x:5:0:sync:/sbin:/bin/sync
7 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
Search for a line with the root keyword/etc/passwd
Copy Code
nl/etc/passwd | Sed '/root/p '
1 Root:x:0:0:root:/root:/bin/bash
1 Root:x:0:0:root:/root:/bin/bash
2 daemon:x:1:1:daemon:/usr/sbin:/bin/sh
3 bin:x:2:2:bin:/bin:/bin/sh
4 sys:x:3:3:sys:/dev:/bin/sh
5 Sync:x:4:65534:sync:/bin:/bin/sync
.... The following ignores
Copy Code
If Root is found, the matching rows are also output in addition to outputting all rows.
When you use-N, only the rows that contain the template are printed.
nl/etc/passwd | Sed-n '/root/p '
1 Root:x:0:0:root:/root:/bin/bash
Search for and delete data
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/sh
3 bin:x:2:2:bin:/bin:/bin/sh
.... The following ignores
#第一行的匹配root已经删除了
Search for data and execute commands
After you find the line that matches the pattern eastern,
Search for/etc/passwd, locate the root row, execute a set of commands in parentheses, and separate each command with a semicolon, where bash is replaced with Blueshell, and then the line is output:
nl/etc/passwd | Sed-n '/root/{s/bash/blueshell/;p} '
1 Root:x:0:0:root:/root:/bin/blueshell
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
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 '
First look at the original information, using/sbin/ifconfig query IP
[Email protected] ~]#/sbin/ifconfig eth0
Eth0 Link encap:ethernet HWaddr 00:90:cc:a6:34:84
inet addr:192.168.1.100 bcast:192.168.1.255 mask:255.255.255.0
Inet6 ADDR:FE80::290:CCFF:FEA6:3484/64 Scope:link
Up broadcast RUNNING multicast mtu:1500 metric:1
..... (omitted below) .....
The IP of this machine is 192.168.1.100.
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
The next step is to delete the following sections, namely: 192.168.1.100 bcast:192.168.1.255 mask:255.255.255.0
Remove the parts that follow the IP
[Email protected] ~]#/sbin/ifconfig eth0 | grep ' inet addr ' | Sed ' s/^.*addr://g ' | Sed ' s/bcast.*$//g '
192.168.1.100
Multi-point editing
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/blueshell
2 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)
Sed can directly modify the contents of a file without using a pipe command 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
Use sed to add "# is a test" directly to the last line of 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!
This article is from "Linux Rookie station" blog, please make sure to keep this source http://10930250.blog.51cto.com/10920250/1717728
sed commands in Linux