Linux commonly used commands sed

table of Contents

1. Grammar

1.1 Common options

1.2 Common commands

1.3 Command principle

2. Examples

2.1 New content---Command a

2.2 Replace content---command c

2.3 Delete content --- delete the specified line

2.4 Insert content---command i

2.5 Replace content---Command s

Three, summary


The Linux sed command uses scripts to process text files line by line. It is a command often used in script processing. Together with awk and grep, it is called the Three Musketeers of Linux. The following will describe with examples.

1. Grammar

sed [options] [script command] [file] ...

1.1 Common options

-e script, --expression=script: Process the input content with the script specified in the option;

-f script-file, --file=script-file: Process the input content with the script-file specified in the option;

1.2 Common commands

a : New content, add text in the specified line;

c : replace the content, use text to replace the content of the specified line;

d  : delete the content, delete the specified line;

i : insert content, insert text in the specified line;

s : Replace content, you can replace the specified content;

Example: the first 15 lines of the /etc/passwd file

root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin

1.3 Command principle

Sed processes the input data row by row, processing one row each time, which can be divided into three steps:

1. Store a copy of the currently processed line in a temporary buffer (called "mode space");

2. Process the data in the current buffer through the sed command;

3. After the data is processed, the data is output to the terminal by default, and then the next line is processed, and all the data has been processed.

The source data is not modified by default.

2. Examples

2.1 New content---Command a

 sed '4a addition some text' passwd

[root@localhost ~]# sed '4a addition some text' passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
addition some text
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost ~]#

In the above example, 4a means inserting content after the fourth line, the following text is the input content, and passwd is the input file. 

2.2 Replace content---command c

sed '2c addition some text' passwd

[root@localhost ~]# sed '2c addition some text' passwd
root:x:0:0:root:/root:/bin/bash
addition some text
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost ~]#

In the above example, 2c means to replace the content of the second line, and the following content is the content to be replaced. 

2.3 Delete content --- delete the specified line

sed '2d' passwd

[root@localhost ~]# sed '2d' passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost ~]#

 sed '2,5d' passwd

[root@localhost ~]# sed '2,5d' passwd
root:x:0:0:root:/root:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost ~]#

 sed '/^bin/d' passwd

[root@localhost ~]# sed '/^bin/d' passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost ~]#

In the above three examples, you can delete by specifying a row, or by specifying an interval, or by specifying specific content for deletion. 

2.4 Insert content---command i

sed '1i addition some text' passwd

[root@localhost ~]# sed '1i addition some text' passwd
addition some text
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost ~]#

The inserted content is different from the newly-added content. Insertion is inserted before the specified row, and newly-added content is inserted after the specified row. 

2.5 Replace content---Command s

sed 's/root/ROOT/g' passwd

[root@localhost ~]# sed 's/root/ROOT/g' passwd
ROOT:x:0:0:ROOT:/ROOT:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/ROOT:/sbin/nologin
[root@localhost ~]#

In the above examples, the specified content can be replaced by commands, just like the replacement in vim. 

Three, summary

The above is just a list of common usages of sed in the form of examples, there are more and more in-depth content, and I will introduce it when I have time.

 

Guess you like

Origin blog.csdn.net/u011074149/article/details/110296542