Use of the Linux sed

Source: Internet
Author: User

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 parameters [-NEFR] action [function] File
Options and Parameters:
-N: Use Quiet (silent) mode. In the usage of general sed, all data from STDIN is generally listed in the
The terminal. However, if you add the-n parameter, only the line (or action) that is specially processed by SED is listed.
-E: The Action of SED is edited directly on the instruction list 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.

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] ~]# 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
..... (omitted later) ... the action of SED 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.
Come on, serve the dishes.
Case 2: Just delete line 2nd
nl/etc/passwd | Sed ' 2d '
Case 3: To delete 3rd to last row
nl/etc/passwd | Sed ' 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/bash
2 Bin:x:1:1:bin:/bin:/sbin/nologin
Drink tea
3 Daemon:x:2:2:daemon:/sbin:/sbin/nologin
..... (omitted later) .....

Case 5: Add "drink tea" before the second line (i.e. the first line) Words

nl/etc/passwd | Sed ' 2i drink tea '
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/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) .....


Each row must be added with a backslash "\" to make the new row. So, in the above example, we can find that in the first

The last side of a line has a \ presence.

Case 6: 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 can replace the entire line of data.


Case 7: 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/nologin
6 Sync:x:5:0:sync:/sbin:/bin/sync
7 Shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
It is possible to select the number of lines in a file by using the display function of the SED in the behavior unit.

Case 7: Search for a line with the root keyword/etc/passwd
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
If Root is found, the matching rows are also output in addition to outputting all rows.


Case 8: 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 9: 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

The first line of the match root has been deleted

Search for data and execute commands
Case 10: Search for/etc/passwd, locate the root row, execute a set of commands in the following curly braces, separating 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

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 that of VI, and the format is as follows: Sed ' s/string to be substituted/new string/g '
Case 11: Querying IP with/sbin/ifconfig

[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) .....

Case 12: 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 13: 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


Modify file Contents directly
Sed can directly modify the contents of the file, but because this action will be directly modified to the original file, so in the modification of the system configuration text
Must be cautious.


Case: 14 use SED to end each line in the Regular_express.txt. Then replace it!
[Email protected] ~]# sed-i ' s/\.$/\!/g ' regular_express.txt
Case 15: 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"
The "-i" option of sed directly modifies the contents of the file, which makes sense in practical use. For example, if
If you have a 1 million-line file, you want to add some text in line 100th, in this case because the file is too large to use VI
Very unrealistic. And the use of sed-i is very convenient.

Use of the Linux sed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.