English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Linux sed command

Linux Command Manual

The Linux sed command is used to process text files with scripts.

sed can process and edit text files according to the instructions of the script.

Sed is mainly used to automatically edit one or more files, simplify repeated operations on files, and write conversion programs, etc.

Syntax

sed [-hnV][-e<script>][-f<script file>][text file]

Parameter description:

  • -e<script> or--expression=<script> Use the script specified in the option to process the input text file.

  • -f<script file> or--file=<script file> Use the script file specified in the option to process the input text file.

  • -h or--help: Display help.

  • -n or--quiet or--silent: Only display the results after script processing.

  • -V or--version: Display version information.

Action description:

  • a: Add, the string after a can be appended, and these strings will appear on a new line (the next line at the moment)~

  • c: Replace, the string after c can be appended, and these strings can replace n1,n2 between lines!

  • d: Delete, because it is a deletion, so nothing is usually appended after d;

  • i: Insert, the string after i can be appended, and these strings will appear on a new line (the previous line at the moment);

  • p: Print, that is, print out some selected data. Usually p is used with sed parameters -n Run together~

  • s: Replace, you can directly perform the replacement work! Usually this s action can be combined with regular expressions! For example 1,20s/old/new/g is it!

Online example

Add a line after the fourth line of the testfile and output the result to standard output. Enter the following command at the command prompt:

sed -e 4a\newLine testfile

First, view the content of testfile as follows:

$ cat testfile #View the content of testfile  
HELLO LINUX!  
Linux is a free unix-type operating system.  
This is a linux testfile!  
Linux test

The output after using the sed command is as follows:

$ sed -e 4a\newline testfile #Add a new string after the fourth line using sed  
HELLO LINUX! #Original content of the testfile  
Linux is a free unix-type operating system.  
This is a linux testfile!  
Linux test  
newline

new addition in units of lines/Delete

Add /etc/The content of passwd is listed and line numbers are printed, at the same time, please also print the line number of the 2~5 line deletion!

[root@www ~]# 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
.....(The rest is omitted).....

line deletion!2,5sed's action is ' 2-5 d' , that d is delete! Because 2-5 lines to be deleted, so the displayed data does not have -e is correct, without -e also works! Also note that the action after sed must be enclosed with '' two single quotes!

Just delete the line 2 Line

nl /etc/passwd | sed '2d'

To delete the line 3 to the last line

nl /etc/passwd | sed '3$d'

Add the word『drink tea?』after the second line (i.e., added to the third line)!

[root@www ~]# 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
.....(The rest is omitted).....

Then if you want to add 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, for example Drink tea or ...... And drink beer?

[root@www ~]# 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
.....(The rest is omitted).....

Each line must be followed by a backslash『 \ 』to add a new line. So, in the above example, we can see that there is a backslash at the end of the first line.

Line-by-line replacement and display

Replace the line2-5The content of the line is replaced with『No 2-5 number』is it?

[root@www ~]# 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
.....(The rest is omitted).....

Through this method, we can replace the entire line of data!

List only /etc/The line number of passwd file is 5-7 Line

[root@www ~]# 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

Through this line-oriented display feature of sed, you can select and display certain lines in a file.

Data search and display

Search /etc/passwd lines with root keyword

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
....Ignore below

If root is found, in addition to outputting all lines, the matching lines will also be output.

Use-When n is used, only lines containing the template will be printed.

nl /etc/passwd | sed -n ''/root/p'
1  root:x:0:0:root:/root:/bin/bash

Data search and delete

Delete/etc/passwd lines containing root, other lines 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
....Ignore below
# The match root in the first line has been deleted

Data search and execute commands

Search/etc/passwd, find the line corresponding to root, execute the set of commands in the brackets, separated by semicolons, here replace bash with blueshell, and output this line:

nl /etc/passwd | sed -n ''/root/{s/bash/blueshell/;p;q'    
1  root:x:0:0:root:/root:/bin/blueshell

The final q is to exit.

Data search and replace

In addition to the line processing mode, sed can also use lines as units for partial data search and replacement. Essentially, sed's search and replace are quite similar to vi! It's a bit like this:

sed 's/the string to be replaced/the new string/g'

First observe the original information, use /sbin/ifconfig query IP

[root@www ~]# /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
.....(The following is omitted).....

The IP of this machine is192.168.1.100.

Delete the part before the IP

[root@www ~]# /sbin/ifconfig eth0 | grep 'inet addr' | sed 's/^.*addr://g'
192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0

Next is to delete the following part, that is to say: 192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0

Delete the part after the IP

[root@www ~]# /sbin/ifconfig eth0 | grep 'inet addr' | sed 's/^.*addr://g' | sed 's/Bcast.*$//g'
192.168.1.100

Multi-point editing

One sed command, delete/etc/The data from the third line to the end of passwd, and replace 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 stands for multi-point editing, the first editing command deletes/etc/The data from the third line to the end of passwd, the second command searches for bash and replaces it with blueshell.

Directly modify file content (dangerous action)

sed can directly modify the content of the file without using pipeline commands or data stream redirection! However, since this action directly modifies the original file, please do not test with system configurations at will! Let's use the file regular_express.txt to test and see it!

The content of the regular_express.txt file is as follows:

[root@www ~]# cat regular_express.txt 
w3codebox.
google.
taobao.
facebook.
zhihu-
weibo-

Use sed to replace the end of each line in regular_express.txt if it ends with . with !

[root@www ~]# sed -i 's/\.$/\!/g' regular_express.txt
[root@www ~]# cat regular_express.txt 
w3codebox!
google!
taobao!
facebook!
zhihu-
weibo-

:q:q

Use sed to directly add to the last line of regular_express.txt # This is a test:

[root@www ~]# sed -i '$a # This is a test' regular_express.txt
[root@www ~]# cat regular_express.txt 
w3codebox!
google!
taobao!
facebook!
zhihu-
weibo-
# This is a test

Since $ represents the last line, and the a action is to add, therefore, the file is added at the end # This is a test!

sed's -i option can directly modify the file content, this feature is very helpful! For example, if you have a 100 lines, you need to add at the 100 lines plus some text, at this time using vim might go crazy! Because the file is too big! What to do? Just use sed! Modify directly through sed/Replacement function, you don't even need to use vim to revise!

Linux Command Manual