Linux sed command

The Linux sed command uses scripts to process text files.

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

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

grammar

sed [-hnV][-e<script>][-f<script文件>][文本文件]

Parameter description :

  • -e<script> or --expression=<script> Process the input text file with the script specified in the option.
  • -f <script file> or --file=<script file> Process the input text file with the script file specified in the options.
  • -h or --help Display help.
  • -n or --quiet or --silent only show the result after script processing.
  • -V or --version Display version information.

Action description :

  • a : new, a can be followed by strings, and these strings will appear on a new line (the current next line)~
  • c : Replace, c can be followed by strings, and these strings can replace the lines between n1 and n2!
  • d : delete, because it is deleted, so there is usually no dong dong after d;
  • i : Insert, i can be followed by strings, and these strings will appear on a new line (the current previous line);
  • p : Print, that is, print out a selected data. Usually p will be run with the parameter sed -n~
  • s : Replace, you can directly carry out the replacement work! Usually the action of this s can be paired with regular notation! For example 1,20s/old/new/g and that's it!

example

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

sed -e 4a\newLine testfile 

First look at the contents of the testfile as follows:

$ cat testfile #查看testfile 中的内容  
HELLO LINUX!  
Linux is a free unix-type opterating system.  
This is a linux testfile!  
Linux test 

After using the sed command, the output is as follows:

$ sed -e 4a\newline testfile #使用sed 在第四行后添加新字符串  
HELLO LINUX! #testfile文件原有的内容  
Linux is a free unix-type opterating system.  
This is a linux testfile!  
Linux test  
newline 

Add/Delete by row

List the contents of /etc/passwd and print the line number, at the same time, please delete lines 2~5!

[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 action of sed is '2,5d', that d is delete! Because lines 2-5 have been deleted for him, the displayed data does not have lines 2-5. Also, please note that sed -e should have been issued originally, and no -e will work! At the same time, it should also be noted that the action followed by sed must be enclosed in two single quotation marks ''!

Just delete line 2

nl /etc/passwd | sed '2d' 

To delete the 3rd to last line

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

Add the words "drink tea?" after the second line (that is, on 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
.....(后面省略).....

Then if it is 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?

[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
.....(后面省略).....

Each line must be added with a backslash " \ " to add a new line! So, in the above example, we can find that \ exists at the end of the first line.

Replacing and Displaying in Lines

What about replacing lines 2-5 with "No 2-5 number"?

[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
.....(后面省略).....

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

List only lines 5-7 in the /etc/passwd file

[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 the display function of this sed in line units, some line numbers in a certain file can be selected and displayed.

Data search and display

Search /etc/passwd for lines with the 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
....下面忽略 

If root is found, in addition to outputting all lines, matching lines are also output.

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

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

Search and delete data

Delete all lines containing root in /etc/passwd, 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
....下面忽略
#第一行的匹配root已经删除了

Search for data and execute commands

Search /etc/passwd, find the line corresponding to root, and execute a set of commands in the curly brackets. Each command is separated by a semicolon. Here, replace bash with blueshell, and then 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 quit.

data search and replace

In addition to the whole-line processing mode, sed can also use behavioral units to search and replace parts of data. Basically sed's search and replace is quite similar to vi! He's kind of like this:

sed 's/要被取代的字串/新的字串/g'

First observe the original information and use /sbin/ifconfig to query the 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 ip of this machine is 192.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

The next step is to delete the subsequent part, that is: 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

multipoint editing

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

Modify the file content directly (dangerous action)

sed can directly modify the contents of a file without having to use piped commands or data stream redirection! However, since this action will directly modify the original file, please do not use the system configuration to test it! Let's test it out with the file regular_express.txt!

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

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

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

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

:q:q

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

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

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

The -i option of sed can directly modify the content of the file, which is very helpful! For example, if you have a file with 1 million lines and you want to add some text on line 100, vim might go crazy! Because the file is too big! What then? Just use sed! With the ability to modify/replace directly with sed, you don't even need to use vim to revise!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324079433&siteId=291194637