linux Advanced -sed succinctly

 

 

linux Advanced -sed succinctly

 Brief introduction

sed is an online editor that processes a row content. Handling, storing the row currently being processed in a temporary buffer, called a "model space" (pattern space), followed by treatment with the contents of the buffer sed command, the processing is completed, the contents of the buffer sent to the screen. Then the next line, which is repeated until the end of the file. File contents not changed, unless you use redirection to store the output. Sed is mainly used to automatically edit one or more documents; simplify repeated operation of the document; write conversion procedures.

 

sed parameters

 

[Root @ www ~] # sed [-nefr] [Action]

Options and parameters:

-n: Use quiet (silent) mode. Sed in general usage, all data from STDIN generally will be listed to the terminal. However, if after adding -n parameter, only through the line sed special treatment (or action) will be listed.

-e: edit sed operates directly on the command-line mode;

-f: sed directly to the action in a written document, -f filename you can run sed action in the filename;

-r: sed action is supported by regular expression syntax type of extension. (The default is the basis for formal notation syntax)

-i: directly modify the contents of the file to read, and not output to the terminal.

 

Action Description: [n1 [, n2]] function

n1, n2: not necessarily exist, usually on behalf of "the number of rows selected for action", for example, if my action is needed between 10-20 row, "10, 20 [action behavior]."

 

function:

a: new, can take back a string, and those strings come (currently the next row) in a new line ~

c: substituted, may be connected to the back c string, these strings can be substituted n1, N2 between the line!

d: Delete, because it is deleted ah, so usually not connected to any later d pat;

i: insert, i can take back the string, which string appears (the line current) is a new line;

p: print, that will print a selection of data. P will usually run together with the parameters sed -n ~

s: replace, the work can be substituted directly miles! Usually this s action can be used with regular expression! For example 1,20s / old / new / g is it!

 

 

To add behavior units / delete

 

The contents of / etc / passwd lists and print the line numbers, meanwhile, please delete the line 2 to 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

..... (hereinafter omitted) .....

 

sed action as '2,5d', that d is deleted! Because the 2-5 line to delete him, so there is no data displayed in the line Lo ~ 2-5 In addition, take note, is supposed to be issued sed -e fishes, no -e will do it! Also note that the rear sed contact operation, in order to make sure '' two single quotes around Oh!

Just delete the line 2

NL / etc / passwd | But 2d '

 

To delete a third to the last line

 NL / etc / passwd | But the '3, $ d'

 

(Ie is added to the third row) plus "drink tea?" In the second line after the 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

..... (hereinafter omitted) .....

 

If it is to be in front of the second row

 nl /etc/passwd | sed '2i drink tea'

 

If you want to increase by more than two lines, add two lines behind the second row, 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

..... (hereinafter omitted) .....

 

Must be a backslash "\" to add a new line between each line of Oh! So, in the example above, we can see that there \ exists at the end face of the first row.

 

To replace the unit with a display behavior

 

The contents of the first line of 2-5 to be replaced by "No 2-5 number" mean?

[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

..... (hereinafter omitted) .....

 

Through this method we will be able to replace the entire line of data!

 

Lists only 5-7 lines 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 this function can display in units of sed, you can be certain the line number within one file selected display.

 

Search and display data

Search / etc / passwd has a root keyword line

 

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 the following ....

 

If the root is found, in addition to all output lines, the output will match the line.

 

Use -n time will only print the line containing the template.

nl /etc/passwd | sed -n '/root/p'

1  root:x:0:0:root:/root:/bin/bash

 

Search for and delete data

Delete the / etc / passwd line contains the root of all 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

Ignore the following ....

# Match the root of the first row has been deleted

 

 

 

Search data and execute commands

Find a matching pattern eastern row after

Search / etc / passwd, find the row corresponding to the root, the braces performed a set of commands back, separated by semicolons between each command, where the replacement is blueshell bash, then the output of the 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 / etc / passwd first bash keyword blueshell, exit

nl /etc/passwd | sed -n '/bash/{s/bash/blueshell/;p;q}'   

1  root:x:0:0:root:/root:/bin/blueshell

Q is the last exit.

 

Search and replace data

In addition to the processing mode of the entire line, sed can also search using partial data in units and substituted. And vi basically quite similar to search and replace of the sed! He is a bit like this:

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

 

The first observation of 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

..... (hereinafter omitted) .....

 

The machine ip is 192.168.1.100.

 

The IP previous section be deleted

[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, the subsequent part is deleted, namely: 192.168.1.100 Bcast: 192.168.1.255 Mask: 255.255.255.0

 

The back of the IP section be deleted

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

192.168.1.100

 

Multi-Edit

A sed command to delete the / etc / passwd third line to the end of the data, and the bash replaced 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 represent multi-point editing, the first edit command deletes the / etc / passwd third line to the end of the data, the second search command bash replaced blueshell.

 

 

Directly modify the contents of the file ( dangerous actions )

 

sed can directly modify the contents of the file, without using a command or data stream pipe redirect! However, this action will modify directly to the original file, so you do not just take system configuration to test! We still use the downloaded file to test regular_express.txt see it!

Use sed to the end of each line if it is within regular_express.txt. Is replaced by!

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

 

Use sed added directly regular_express.txt last line "# This is a test."

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

Since the $ represents the last line, but a movement is new, so the final document to add "# This is a test"!

sed the "-i" option can directly modify the contents of the file, which is very helpful! For example, if you have a million lines of files you want to add some text on line 100, this time using vim might be mad! Because the file is too big! How to do that? Sed on the use of ah! Modified directly through sed / replace function, you do not even need to use vim to amend!

 

Guess you like

Origin www.cnblogs.com/PaulTsao/p/11705082.html