linux shell - sed Detailed

sed is a good file processing tool itself is a command pipeline, mainly in units of processing, data lines can be replaced, delete, add, and other select specific job. 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 command line format is:  sed [-nefri] 'Command' input text .

Common options:

        -n: use the quiet (silent) mode. In general usage sed, all data from STDIN generally will be listed on the screen. However, if after adding -n parameter, only the row sed through special handling (or operation) will be listed;

        -e: sed directly edit operation in the command line mode;

        -f: sed directly to the action in writing within a file, -f filename sed action can be performed within the filename;

        -r:sed action is the syntax of extended-support regular expression (the default is the basis of regular expression syntax);

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

Command Description: [N1 [, n2]] the Command

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 [ the Command ]."

Commonly used commands:

        a: new, can take back a string, and those strings come (now the next line) on 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 the choice of a data printout. P will usually work 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 that g is added to replace all, without g replace only the first occurrence of the character.

 

Command Function

A \ added after the current row or line of text.

c \ change or replace the text with the new text of the Bank.

d delete rows from the pattern space location.

I \ Insert text above the current line.

h copying the contents of the pattern space holding buffer (special buffer).

H is added to the contents of pattern space holding buffer.

g of obtaining content holding buffer, and replacing the current text in the pattern space.

G obtained in the content holding buffer and appended to the current pattern space.

N reading a next input row, the row with the new process the next command instead of the first command.

p print pattern space in the line.

P to print the first row of the pattern space.

q to quit sed.

w file write and append pattern space to the end of the file.

! Shows the effect occurs for all subsequent commands are not selected rows.

s / re / string string is replaced with a regular expression re.

= Print the current line number.

Replace mark  

Full replacement within the g-line, if not g, only replace the first match.

p print line.

x swap text and pattern space in the holding buffer.

y to translate a character to another character (but not for regular expressions).

Options  

-e allows multi-point editing.

-n cancel the default output.

 

First, in units of add / 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' 

 

After the second line (ie, is added to the third row) plus "drink tea" word!

[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.

 

Second, to replace the units of the display

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.

 

Third, the data search and display

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

 

Fourth, 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

 

Fifth, the search for data and execute commands

After finding the pattern matching row, the search / etc / passwd, find the row corresponding to the root, a set of braces back command, each command separated by semicolons, where the replacement is blueshell bash, then the output 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.

 

Sixth, find 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.

 

grep pattern [file...]

Search All position pattern appears in the file, either string pattern to search for, it can be a regular expression.

 

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

 

Seven multi-point editing

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.

 

Eight, to 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!

 

reference:

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

 

Other examples: (Suppose we have a file called ab)

Delete a row

[Root @ localhost ruby] # sed '1d' ab # delete the first row 

[Root @ localhost ruby] # sed '$ d' ab # delete the last line

[Root @ localhost ruby] # sed '1,2d' ab # delete the first row to the second row

[Root @ localhost ruby] # sed '2, $ d' ab # delete the second to the last row

Display a row

[Root @ localhost ruby] # sed -n '1p' ab # displays the first line 

[Root @ localhost ruby] # sed -n '$ p' ab # display the last line

[Root @ localhost ruby] # sed -n '1,2p' ab # displaying a first to a second line

[Root @ localhost ruby] # sed -n '2, $ p' ab # display the second to the last row

Use query mode

[Root @ localhost ruby] # sed -n '/ ruby ​​/ p' ab # query all the rows where the ruby ​​including keyword

[Root @ localhost ruby] # sed -n '/ \ $ / p' ab # $ query includes keywords where all the lines, using backslash \ shielded special meaning

Increasing one or more lines of character strings

[root@localhost ruby]# cat ab

     Hello!

     ruby is me,welcome to my blog.

     end

[Root @ localhost ruby] # sed '1a drink tea' increases after the first line character string ab # "drink tea"

     Hello!

     drink tea

     ruby is me,welcome to my blog. 

     end

[Root @ localhost ruby] # sed '1,3a drink tea' ab # first to third rows increases after the string "drink tea"

     Hello!

     drink tea

     ruby is me,welcome to my blog.

     drink tea

     end

     drink tea

[Root @ localhost ruby] # sed '1a drink tea \ nor coffee' ab # after first row have multiple lines, line breaks \ n

     Hello!

     drink tea

     or coffee

     ruby is me,welcome to my blog.

     end

Instead of one or more rows

[Root @ localhost ruby] # sed '1c Hi' ab # instead of the first line is Hi

     Hi

     ruby is me,welcome to my blog.

     end

[Root @ localhost ruby] # sed '1,2c Hi' ab # first to a second line in place of the Hi

     Hi

     end

 

Alternatively some portion of line

Format: sed 's / to replace the string / new string / g' (string can be replaced with a regular expression)

[root@localhost ruby] # sed -n '/ruby/p' ab | sed 's/ruby/bird/g'    #替换ruby为bird

[root@localhost ruby] # sed -n '/ruby/p' ab | sed 's/ruby//g'        #删除ruby

 

insert

[Root @ localhost ruby] # sed -i '$ a bye' ab # direct input in the last line in the file ab "bye"

[root@localhost ruby]# cat ab

     Hello!

     ruby is me,welcome to my blog.

     end

     bye

Print: p command

sed '/ abc / p' file contained in file abc print line. By default, all rows sed to print to the screen, if a line to match the pattern, put the bank an additional print again

sed -n '/ abc / p' file and the same as above, but remove the default behavior of sed will only print matching lines

Delete: d command

sed '3, $ d' file to remove content from the third line to the last line.

sed '$ d' file delete the contents of the last line

sed '/ abc / d' abc contains the deleted rows.

sed '3d' file delete the contents of the third line

Replace: s command

sed 's / abc / def / g' file to replace all the lines into the abc def, if not g, only replace the first in the row abc

sed -n 's / abc / def / p' file to print only those rows of a substitution

sed 's / abc / & def /' file is added later in all def abc (& representing the contents match)

sed -n 's / abc / def / gp' file to replace all abc def, and prints a substitution of those lines

sed 's # abc # def # g' file to replace all abc DEF, with the latter in alternative s character is to find the division between the character string and the replacement string, in this case #

Range specified line: comma

sed -n '/ abc /, / def / p' file to the print mode abc def line

sed -n '5 /, / def / p' file containing print from the fifth row to row between rows def.

sed / abd /, / def / s / aaa / bbb / g modified line between the mode from the mode def abc, def to replace aaa

Multi Edit -e

sed -e '1,3d' -e 's / abc / def / g' file delete rows 1-3, and then replace the remaining rows into abc def

Reading file: r command

sed '/ abc / r newfile' file after the line containing the abc and reading contents of newfile

Write file: w command 

sed '/ abc / w newfile' file is written in the line containing newfile of abc

Append: a command

sed '/ abc / a \ def' file after the line containing the new line abc, def write

Insert: i command 

sed '/ abc / i \ def' file before the line containing the new line abc, def write

Modify: c command

sed '/ abc / c \ def' file is replaced in the row containing abc def, the old text is covered

Reads the next line: n Command   

sed '/ abc / {n; s / aaa / bbb / g;}' file read the next line containing abc, replacing aaa to bbb

Conversion: y command 

sed 'y / abc / ABC' file will be replaced by a A, b replaced by B, c replaced with C (regular expression character element work)

Exit: q command

sed '/ abc / {s / aaa / bbb /; q;}' file is included in a line abc, replace aaa to bbb, then exit sed.

Guess you like

Origin blog.csdn.net/xiebingsuccess/article/details/91861570