Examples of getting started with SED

Source: Internet
Author: User
Tags control characters

Syntax format

sed [-nefir] ' command ' file (s)

sed [-nefir]-F scriptfile file (s)

Common Options :

-N: Use Quiet (silent) mode. In the usage of the general sed, sed automatically prints the contents of the mode space to the screen after all script instructions have been executed. However, if you add the-n parameter, only the line (or action) that is specially processed by SED is listed.

-E: The Action of SED is edited directly on the instruction list mode;

-F: The action of SED is written directly in a file, and-f filename can perform the SED action within filename;

-r:sed's actions support the syntax of extended formal notation. (Presupposition is the basic formal notation of French law)

-I: Directly modify the contents of the read file instead of the screen output.


Common directives:

name Command Grammar Description
Replace S [Address]s/pattern/replacement/flags Replace matching content
Delete D [Address]d Delete a matching row
Insert I [Line-address]i\

Text
Insert text in front of the matching line
Additional A [Line-address]a\

Text
Insert text behind a matching line
Line substitution C [Address]c\

Text
Replace matching rows with text
Print Line P [Address]p Print lines in pattern space
Print line number = [address]= Print the current line line number
Print Line L [Address]l Prints lines in the pattern space while displaying control characters
Convert characters Y [address]y/set1/set2/ Replace the characters appearing in SET1 with the corresponding positions in the SET2
Read Next line N [Address]n Reads the contents of the next line into the pattern space
Read the file R [Line-address]r File After the specified file is read to the matching line
Write a file W [Address]w File Output all rows of the matching address to the specified file
Exit Q [Line-address]q Exits after a matching line is read


The text test.txt content to be processed is as follows:

Device=eth1hwaddr=00:0c:29:c5:ae:edtype=ethernetuuid=bb4e221b-6953-4bfd-9842-fd442c02ae8eonboot=yesnm_ Controlled=yesbootproto=dhcpdns1=202.96.18.166ipv6init=nouserctl=nopeerdns=yes

SED options

a append:

Example

add a row after the third line with the content ipv4=192.168.1.1

sed ' 3a ipv4=192.168.1.1 ' test.txt

Results

Device=eth1hwaddr=00:0c:29:c5:ae:edtype=ethernetipv4=192.168.1.1uuid= bb4e221b-6953-4bfd-9842-fd442c02ae8eonboot=yesnm_controlled=yesbootproto=dhcpdns1=202.96.18.166ipv6init= Nouserctl=nopeerdns=yes

I Insert:

Example

add a row before the third line with the content ipv4=192.168.1.1

sed ' 3i ipv4=192.168.1.1 ' test.txt

Results

Device=eth1hwaddr=00:0c:29:c5:ae:edipv4=192.168.1.1type=ethernetuuid= bb4e221b-6953-4bfd-9842-fd442c02ae8eonboot=yesnm_controlled=yesbootproto=dhcpdns1=202.96.18.166ipv6init= Nouserctl=nopeerdns=yes

s Replacement:

Example

change all the yes to no

sed ' s/yes/no/g ' test.txt

Results

Device=eth1hwaddr=00:0c:29:c5:ae:edtype=ethernetuuid=bb4e221b-6953-4bfd-9842-fd442c02ae8eonboot=nonm_ Controlled=nobootproto=dhcpdns1=202.96.18.166ipv6init=nouserctl=nopeerdns=no

Example

Add # At the beginning of each line

Sed ' s/^/#&/g ' test.txt

Results

#DEVICE =eth1#hwaddr=00:0c:29:c5:ae:ed#type=ethernet#uuid=bb4e221b-6953-4bfd-9842-fd442c02ae8e#onboot=yes#nm_ Controlled=yes#bootproto=dhcp#dns1=202.96.18.166#ipv6init=no#userctl=no#peerdns=yes

Add # At the end of each line

sed ' s/$/&#/g ' test.txt

Results

Device=eth1#hwaddr=00:0c:29:c5:ae:ed#type=ethernet#uuid=bb4e221b-6953-4bfd-9842-fd442c02ae8e#onboot=yes#nm_ controlled=yes#bootproto=dhcp#dns1=202.96.18.166#ipv6init=no#userctl=no#peerdns=yes#



In addition to the third row, the end of all lines plus #

[[Email protected] ~]# sed ' 3! S/$/&#/g ' test.txtdevice=eth1#hwaddr=00:0c:29:c5:ae:ed#type=ethernetuuid= bb4e221b-6953-4bfd-9842-fd442c02ae8e#onboot=yes#nm_controlled=yes#bootproto=dhcp#dns1=202.96.18.166#ipv6init= no#userctl=no#peerdns=yes#

Exclude the line where the IPV6 is located, plus the end of the other line

[[Email protected] ~]# sed '/ipv6/!s/$/&#/g ' test.txtdevice=eth1#hwaddr=00:0c:29:c5:ae:ed#type=ethernet#uuid= bb4e221b-6953-4bfd-9842-fd442c02ae8e#onboot=yes#nm_controlled=yes#bootproto=dhcp#dns1=202.96.18.166#ipv6init= nouserctl=no#peerdns=yes#




Advanced Applications

p and P

P: Print the current mode space content, append to the default output;

P: Print the contents of the current mode space beginning to \ n and append to the default output;

N and N

The-n command simply means reading the next line ahead of time, overwriting the front row of the model space (which is not deleted, and therefore still printing to standard output), and if the command does not succeed (not skipping: the front-end condition does not match), discard any commands after it, and perform the SED on the newly read content.


n the processing flow is as follows:


Example

[email protected] ~]# cat num.txt12345


Example

[email protected] ~]# cat num.txt12345

Print even rows


Sed-n ' N;p ' Num.txt24

Analytical:

Read 1 to Pattern space

Execute Command N

Read 2 and overwrite to pattern space

Execute Command P

Output 2


Execute Next Loop immediately

Read 3 to Pattern space

Execute Command N

Read 4 and overwrite to pattern space

To execute the command p, the procedure is as follows

Output 4

The last line, 5 of the specific process is as follows


Read 5-in mode space


Run command N, but cannot read the next line


Sed exits all commands because it cannot be read


is already at the end of the file and sed is running.



Printer Odd Line

Sed-n ' $! N P ' num.txt135

Analytical

Read 1,$! condition satisfies (not tail line), executes n command, obtains 1\n2, executes p, prints 1, reads 3,$! condition satisfies (not tail line), executes n command, draws 3\n4, executes p, prints 3, reads 5,$! condition is not satisfied, skips N, executes p, prints 5

D and D

D: Deletes the current mode space content (not passed to standard output), discards the command after it, and executes the SED again on the newly read content.

D: Delete the contents of the current mode space beginning to \ n (not passed to standard output), discard the following command, but re-execute sed on the remaining mode space.

[email protected] ~]# cat num.txt12345

Print odd lines

[[Email protected] ~]# sed ' n;d ' num.txt135

Analytical:

Read 1, execute n, print 1 to standard output, draw 2, execute D, delete 2, hollow, and so on, read 3, execute N, print 3 to standard output, draw 4, execute D, delete 4, hollow, but read 5 o'clock, because N cannot execute, so D does not execute. Because there is no-n parameter, so the output 1\n3\n5;


Read the last line of text

[[Email protected] ~]# sed ' n;d ' num.txt5

Note: Read 1, execute n, Draw 1\n2, execute D, draw 2, execute N, Draw 2\n3, execute D, draw 3, and so on, draw 5, execute N, conditional failure exit, because no-n parameter, so output 5

Note:

The d command executes the next loop directly after it is run, so it does not execute subsequent command and print mode space, for example, using a----after the D command will not execute.

[[Email protected] ~]# sed ' n;d;a----' num.txt135

h and H G and G


H: Overwrite content in current mode space to hold space

H: Append the contents of the current pattern space to the hold space

G: Overwrite content in current hold space to pattern space

G: Append the contents of the current hold space to the mode space


Example

[email protected] ~]# cat num.txt12345

Use H alone

[[Email protected] ~]# sed  ' h ' num.txt12345

Copy the current content to the hold space, and only the content of the mode space is output by default;

[[Email protected] ~]# sed  ' G ' num.txt12345

Appends the contents of the hold space to the current pattern space, since the space is empty, so the appended result is to add a blank line after each line;


Example


Output all rows to the bottom

[[Email protected] ~]# sed ' 1! G;h;$!d ' num.txt54321

Analytical:

Read the first line; Match 1! G, so that the contents of the space will not be copied to the schema space, the current content is 1, the first line of the content (1) to maintain space; execution $!d is not the last line, delete the schema space content;

After the first line executes the result: The schema space content is empty, keeping the space content 1 ()

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7D/E5/wKioL1byW3yS8F3SAAAWIyjx3Ho823.jpg "title=" 122222.jpg "alt=" Wkiol1byw3ys8f3saaawiyjx3ho823.jpg "/>

Read the second line, execute 1! G, match 1! G, append space to the pattern space

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/7D/E8/wKiom1byW6SCZrf5AAAbddXXlYs257.jpg "title=" 122222.jpg "alt=" Wkiom1byw6sczrf5aaabddxxlys257.jpg "/>

Execute h Copy Mode space content to hold space

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/7D/E5/wKioL1byXiuzyEVzAAAYov-hUjQ352.jpg "title=" 122222.jpg "alt=" Wkiol1byxiuzyevzaaayov-hujq352.jpg "/>


Execution $!d is not the last line, delete

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7D/E5/wKioL1byXo-h5wz4AAAYQzVHz2Y688.jpg "title=" 122222.jpg "alt=" Wkiol1byxo-h5wz4aaayqzvhz2y688.jpg "/>


Read the third line, execute 1! G, match 1! G, append space to the pattern space

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/7D/E8/wKiom1byXySjLa2nAAAag8A3r_s389.jpg "title=" 122222.jpg "alt=" Wkiom1byxysjla2naaaag8a3r_s389.jpg "/>

Execute h Copy Mode space content to hold space

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7D/E5/wKioL1byYBSDnceNAAAbMh5LgUU386.jpg "title=" 122222.jpg "alt=" Wkiol1byybsdncenaaabmh5lguu386.jpg "/>

Execution $!d is not the last line, delete

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/7D/E5/wKioL1byYFrRBLU4AAAZ7KTpHvs313.jpg "title=" 122222.jpg "alt=" Wkiol1byyfrrblu4aaaz7ktphvs313.jpg "/>

Read line fourth, execute 1! G, match 1! G, append space to the pattern space

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7D/E9/wKiom1byYEPRDfL8AAAgJ8IJea0321.jpg "title=" 122222.jpg "alt=" Wkiom1byyeprdfl8aaagj8ijea0321.jpg "/>

Execute h Copy Mode space content to hold space

650) this.width=650; "src=" http://s5.51cto.com/wyfs02/M00/7D/E9/wKiom1byYa7hz0iWAAAdbaIdoqU545.jpg "title=" 3.jpg " alt= "Wkiom1byya7hz0iwaaadbaidoqu545.jpg"/>

Execution $!d is not the last line, delete

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/7D/E9/wKiom1byYf_wUmGaAAAcCF2FCb4109.jpg "title=" 3.jpg " alt= "Wkiom1byyf_wumgaaaaccf2fcb4109.jpg"/>

Read the last line, execute 1! G, match 1! G, append space to the pattern space

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/7D/E6/wKioL1byY0zwRRm_AAAfEkyhiI8530.jpg "title=" 3.jpg " alt= "Wkiol1byy0zwrrm_aaafekyhii8530.jpg"/>

Execute h Copy Mode space content to hold space

650) this.width=650; "src=" http://s2.51cto.com/wyfs02/M01/7D/E9/wKiom1byYyWBwhF_AAAkidmpGsE265.jpg "title=" 3.jpg " alt= "Wkiom1byyywbwhf_aaakidmpgse265.jpg"/>

Because it is the last line, do not execute $!d; the results are as follows

[[Email protected] ~]# sed ' 1! G;h;$!d ' num.txt54321


Example

[email protected] ~]# cat num.txt12345

Add ";" in the penultimate line

[[Email protected] ~]# sed ' $! n;$! p;$! D;s/\ (\n\)/;\n/' Num.txt 1234;5

Analytical:

First line 1 read, match $! N; become a 1\n2,

Match $! P; become

1

1\n2

Match $! D

Become

1

2

Because there is no \ n, s/\ (\n\) \;\n/This article does not carry out;

Second read-in, matching $! N; become

1

2\n3

Match $! P; become

1

2

2\n3

Match $! D

Become

1

2

3

Third read-in, matching $! N; become

1

2

3\n4

Match $! P; become

1

2

3

3\n4

Match $! D

Become

1

2

3

4

Third read-in, matching $! N; become

1

2

3

4\n5

does not match $! P; so do not perform

Mismatch matching $! D

Match to S/\ (\n\)/;\n/so the back is added ";"

Sed ' $! n;$! p;$! D;s/\ (\n\)/;\n/' Num.txt


Replace S [Address]s/pattern/replacement/flags Replace matching content
Delete D [Address]d Delete a matching row
Insert I [Line-address]i\

Text
Insert text in front of the matching line
Additional A [Line-address]a\

Text
Insert text behind a matching line
Line substitution C [Address]c\

Text
Replace matching rows with text
Print Line P [Address]p Print lines in pattern space
Print line number = [address]= Print the current line line number
Print Line L [Address]l Prints lines in the pattern space while displaying control characters
Convert characters Y [address]y/set1/set2/ Replace the characters appearing in SET1 with the corresponding positions in the SET2
Read Next line N [Address]n Reads the contents of the next line into the pattern space
Read the file R [Line-address]r File After the specified file is read to the matching line
Write a file W [Address]w File Output all rows of the matching address to the specified file
Exit Q [Line-address]q Exits after a matching line is read

This article is from the "Please give me a knife" blog, please be sure to keep this source http://lulucao2006.blog.51cto.com/5375246/1754721

Examples of getting started with SED

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.