linux sed command

Introduction

sed is an online editor that processes content one line at a time. When processing, store the currently processed line in a temporary buffer, called "pattern space", and then use the sed command to process the contents of the buffer. After the processing is complete, send the contents of the buffer to the screen. Then the next line is processed, and so on, until the end of the file. The file contents are not changed unless you use redirection to store the output. Sed is mainly used to automatically edit one or more files; simplify repeated operations on files; write conversion programs, etc.


sed uses parameters
Copy code

[root@www ~]# sed [-nefr] [action]
options and parameters:
-n: Use silent mode. In normal sed usage, all data from STDIN would normally be listed on the terminal. But if you add the -n parameter, only the line (or action) that has been specially processed by sed will be listed.
-e : Edit sed actions directly in command line mode;
-f : Write sed actions directly in a file, -f filename can run sed actions in filename;
-r : sed actions support The syntax of extended regular notation. (default is basic regular notation syntax)
-i : Directly modifies the contents of the file read, rather than outputting to the terminal.

Action description: [n1[,n2]]function
n1, n2 : It may not exist, generally it means "select the number of lines to perform the action", for example, if my action needs to be performed between 10 and 20 lines , then the "10,20[action behavior]"

function:
a : new, a can be followed by a string, and these strings will appear on a new line (the current next line) ~
c: replaced, c can be followed by a string, and these strings can replace n1, n2 between the lines!
d : delete, because it is delete, 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 : Printing, that is, printing out the selected data. Usually p will run with the parameter sed-n ~
s: replace, you can directly replace the work! Usually the action of this s can be paired with regular notation! For example 1,20s/old/new/g and that's it!

Copy code Add/delete


in line units


List the contents of /etc/passwd and print the line number, and 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
.....(Omitted later).....


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 the last line

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



add "drink tea" after the second line (that is, add it to the third line) ?” words!

[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
.....(Omitted later)..... if it is nl /



before the second line

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

[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

3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
.....(Omitted later).....

Copy code

Each line must be preceded by a backslash " \ " The addition of a new line! So, in the above example, we can find that \ exists at the end of the first line.

Replacing and displaying in line


units What about replacing the content of 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
.....(Omitted later).....


Through this method, we can replace the entire line 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 With the display function in line unit, some line numbers in a certain file can be selected and displayed.

Data search and display

Search /etc/passwd for lines with root keyword
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
....

Ignored

below root found, in addition to outputting all lines, it will also output matching lines.



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


data search and delete

Delete all lines containing root in /etc/passwd, other Line 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 the following
# The matching root of the first line has been deleted. Search for






data and execute the command

. After finding the line matching the pattern eastern,

search /etc/passwd, find the line corresponding to root, and execute the set of curly brackets that follow. Commands, 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}'
1 root :x:0:0:root:/root:/bin/blueshell

If you only replace the first bash keyword of /etc/passwd with 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 to exit.


Data Search and Replace

In addition to the whole-line processing mode, sed can also perform partial data search and replace in behavioral units. Basically sed's search and replace is quite similar to vi! It's a bit like this:

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



Look at the original information first, 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
.....( Omitted below).....


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


Multi-point editing

A sed command, delete the third line to the end of /etc/passwd data, and replace bash with blueshell

nl /etc/passwd | sed -e '3,$d'
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 Delete 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 contents of the file directly (dangerous action)


sed can directly modify the contents of the file without using pipe 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 downloaded regular_express.txt file!

Use sed to change the end of each line in regular_express.txt to .

[root@www ~]# sed -i 's/\.$/\!/g' regular_express.txt



uses sed directly at the end of regular_express.txt A line is added to "# This is a test"

[root@www ~]# sed -i '$a # This is a test' regular_express.txt

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

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!



Reference http://vbird.dic.ksu.edu.tw/linux_basic/0330regularex_2.php#sed

       http://www.cnblogs.com/stephen-liu74/archive/2011/11/17/2245130.html

Guess you like

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