The use of SED under Linux

Source: Internet
Author: User

This article mainly explains:

Processing of---sed text blocks



I. Processing of SED text blocks


1. Basic usage of SED text block processing


Common processing options are: Insert text before line I

Insert text after line a

C Replace when forward

When you need to insert multiple lines of text, one method is to "\ n" for wrapping, and the other to be delimited with "\". This method may be more consistent with reading habits.


Use "&" to invoke the entire lookup string in the S-replace operation


Or with a test document: Rclocal.txt

[email protected] ~]# cat Rclocal.txt

1 #!/bin/sh

2 #

3 # This script is executed *after* all and the other init scripts.

4 # You can put your own initialization stuff on here if you don ' t

5 # Want to does the full Sys V style init stuff.

6

7 touch/var/lock/subsys/local


1) Insert text before line I

Insert a line of "insert before" string before line 3rd:

[[Email protected] ~]# sed ' 3iInsert before ' rclocal.txt

1 #!/bin/sh

2 #

Insert before

3 # This script is executed *after* all and the other init scripts.

4 # You can put your own initialization stuff on here if you don ' t

5 # Want to does the full Sys V style init stuff.

6

7 touch/var/lock/subsys/local

Insert a line of "insert before" string before the last line:

[[Email protected] ~]# sed ' $iInsert before ' rclocal.txt

1 #!/bin/sh

2 #

3 # This script is executed *after* all and the other init scripts.

4 # You can put your own initialization stuff on here if you don ' t

5 # Want to does the full Sys V style init stuff.

6

Insert before

7 touch/var/lock/subsys/local

Insert a line "XXXX" string before each line that contains "Init":

[[Email protected] ~]# sed '/init/ixxxx ' rclocal.txt

1 #!/bin/sh

2 #

. xxxx

3 # This script is executed *after* all and the other init scripts.

Xxxx

4 # You can put your own initialization stuff on here if you don ' t

Xxxx

5 # Want to does the full Sys V style init stuff.

6

7 touch/var/lock/subsys/local

2) Insert text after line a

Insert a row of "insert after" strings after line 3rd:

[[Email protected] ~]# sed ' 3aInsert after ' rclocal.txt

1 #!/bin/sh

2 #

3 # This script is executed *after* all and the other init scripts.

Insert after

4 # You can put your own initialization stuff on here if you don ' t

5 # Want to does the full Sys V style init stuff.

6

7 touch/var/lock/subsys/local

Append a line of "Insert after" strings after the last line:

[[Email protected] ~]# sed ' $aInsert after ' rclocal.txt

1 #!/bin/sh

2 #

3 # This script is executed *after* all and the other init scripts.

4 # You can put your own initialization stuff on here if you don ' t

5 # Want to does the full Sys V style init stuff.

6

7 touch/var/lock/subsys/local

Insert after

Insert a line "XXXX" string after each line that contains "stuff":

[[Email protected] ~]# sed '/stuff/axxxx ' rclocal.txt

1 #!/bin/sh

2 #

3 # This script is executed *after* all and the other init scripts.

4 # You can put your own initialization stuff on here if you don ' t

Xxxx

5 # Want to does the full Sys V style init stuff.

Xxxx

6

7 touch/var/lock/subsys/local


3) Replace current line C

Replace the entire line of line 1th with "#!/bin/bash":

[[Email protected] ~]# sed ' 1c#!/bin/bash ' rclocal.txt

#!/bin/bash

2 #

3 # This script is executed *after* all and the other init scripts.

4 # You can put your own initialization stuff on here if you don ' t

5 # Want to does the full Sys V style init stuff.

6

7 touch/var/lock/subsys/local

Replace line 1th to 4th whole with "#!/bin/bash":

[[Email protected] ~]# sed ' 1,4c#!/bin/bash ' rclocal.txt

#!/bin/bash

5 # Want to does the full Sys V style init stuff.

6

7 touch/var/lock/subsys/local

Replace each line that contains "/bin/sh" with "#!/bin/bash", respectively:

[[Email protected] ~]# sed '/\/bin\/sh/c#!/bin/bash ' rclocal.txt

#!/bin/bash

2 #

3 # This script is executed *after* all and the other init scripts.

4 # You can put your own initialization stuff on here if you don ' t

5 # Want to does the full Sys V style init stuff.

6

7 touch/var/lock/subsys/local


Replace each line that contains the word "init" with "#!/bin/bash":

[[Email protected] ~]# sed '/\<init\>/c#!/bin/bash ' rclocal.txt

1 #!/bin/sh

2 #

#!/bin/bash

4 # You can put your own initialization stuff on here if you don ' t

#!/bin/bash

6

7 touch/var/lock/subsys/local

Note:\< indicates that a word has started, \> means that a word has ended \<init\> the whole word, like initital is not consistent with the results of **************************** *****


4) Processing of multiple lines of text


When you need to insert multiple lines of text, one method is to "\ n" for wrapping, and the other to be delimited with "\". This method may be more consistent with reading habits. To insert three lines of text before line 3rd, with the contents followed by "xxxx", "yyyy", and "zzzz" as an example, the following actions can compare the effects of two methods:


[[Email protected] ~]# sed ' 3ixxxx\nyyyy\nzzzz ' rclocal.txt// Method 1

1 #!/bin/sh

2 #

Xxxx

yyyy

Zzzz

3 # This script is executed *after* all and the other init scripts.

4 # You can put your own initialization stuff on here if you don ' t

5 # Want to does the full Sys V style init stuff.

6

7 touch/var/lock/subsys/local

[[Email protected] ~]# sed ' 3ixxxx\

> yyyy\

> zzzz ' rclocal.txt// Method 2

1 #!/bin/sh

2 #

Xxxx

yyyy

Zzzz

3 # This script is executed *after* all and the other init scripts.

4 # You can put your own initialization stuff on here if you don ' t

5 # Want to does the full Sys V style init stuff.

6

7 touch/var/lock/subsys/local

2. Adjust system configuration using SED text block processing


1) Modify host name


The hostname configuration file is located in/etc/sysconfig/network, and the hostname setting starts with "HOSTNAME".

Before modification:

[Email protected] ~]# cat/etc/sysconfig/network

Networking=yes

Networking_ipv6=no

Hostname=svr5.tarena.com

Replace the line that begins with "HOSTNAME" with the whole line, set to "hostname=mysvr.example.org":

[Email protected] ~]# sed-i '/^hostname/chostname=mysvr.example.org '/etc/sysconfig/network

Confirm the replacement result:

[Email protected] ~]# cat/etc/sysconfig/network Networking=yes

Networking_ipv6=no

hostname=mysvr.example.org

The following actions can be restored:

[Email protected] ~]# sed-i '/^hostname/chostname=svr5.tarena.com '/etc/sysconfig/network

[Email protected] ~]# cat/etc/sysconfig/network Networking=yes

Networking_ipv6=no

Hostname=svr5.tarena.com

2) Add hosts host mapping record

Add 2 mapping records for the task required after the last line of the/etc/hosts file:

[Email protected] ~]# sed-i ' $a 192.168.4.5 svr5.tarena.com svr5\

> 119.75.217.56 www.baidu.com '/etc/hosts

To verify the Add Effect:

[Email protected] ~]# Tail-2/etc/hosts

192.168.4.5 svr5.tarena.com SVR5

119.75.217.56 www.baidu.com



2. Sed Text processing exercises

Start by creating a test file that contains English paragraphs, such as/etc/nsswitch.conf files. To make it easier to see the effect, we'll take line 4th to 10th from this file and remove the "#" from the beginning. The beginning of the 10 lines is as follows:


[Email protected] ~]# head-10/etc/nsswitch.conf

#

#/etc/nsswitch.conf

#

# An example Name Service Switch config file. This file should is

# sorted with the most-used services at the beginning.

#

# The entry ' [Notfound=return] ' means that's the search for an

# entry should stop if the search in the previous entry turned

# up to Nothing. Note If the search failed due to some and other reason

# (like no NIS server responding) and the search continues with the


The interception operation and results are as follows:

[Email protected] ~]# sed-n ' 4,10p '/etc/nsswitch.conf | Sed ' s/#//' > Nssw.txt

[email protected] ~]# cat Nssw.txt

An example Name Service Switch config file. This file should is

Sorted with the most-used services at the beginning.

#

The entry ' [Notfound=return] ' means that the search for an

Entry should stop if the search in the previous entry turned

Up nothing. Note If the search failed due to some and other reason

(like no NIS server responding) and the search continues with the

The action in this section is to use Nssw.txt as the test file.

1) Delete the second and last character of each line in the file.

Replace two times, replace the 2nd character for the first time, and replace the last character for the second time:

[[Email protected] ~]# sed ' s/.//2;s/.$//' nssw.txt

A example Name Service Switch config file. This file should b

Srted with the most-used services at the beginning

#

Te entry ' [Notfound=return] ' means that the search for a

Etry should stop if the search in the previous entry Turne

U nothing. Note If the search failed due to some and other Reaso

(IKE no NIS server responding) then the search continues with th



2) Delete the second and last word of each line in the file.


Replace two times, replace the 2nd word for the first time, and replace the last word the second time:

[[email protected] ~]# sed-r ' s/[a-z]+//2;s/[a-z]+ ([^a-z]*) $/\1/' Nssw.txt

An Name Service Switch config file. This file should

Sorted the most-used services at the.

#

The ' [Notfound=return] ' means that's the search for

Entry stop if the search in the previous entry

Up. Note If the search failed due to some other

(like NIS server responding) and the search continues with

3) Swaps the first and second characters of each line in a file.

Each line of text is split into "1th character", "2nd character", "All remaining characters" three parts, and then the replacement operation is "2-1-3" in the Order of rearrangement:

[[email protected] ~]# sed-r ' s/^ (.) (.) (. *)/\2\1\3/' Nssw.txt

NA example Name Service Switch config file. This file should is

Osrted with the most-used services at the beginning.

#

HTe entry ' [Notfound=return] ' means that the search for an

Netry should stop if the search in the previous entry turned

Pu nothing. Note If the search failed due to some and other reason

L (Ike n up. Note If the search failed due to some other

(like NIS server responding) and the search continues with


4) Swap the first and second words of each line in the file.


Each line of text is split into "1th word", "word delimited", "2nd word", "all remaining characters" four parts, and then the order of substitution is "3-2-1-4":


[[email protected] ~]# sed-r ' s/([a-z]+) ([^a-z]*] ([a-z]+) (. *)/\3\2\1\4/' nssw.txt example an Name Service Switch config F Ile. This file should is

With sorted, the most-used services at the beginning.

#

Entry the ' [Notfound=return] ' means that the search for an

Should entry stop if the search in the previous entry turned

Nothing up. Note If the search failed due to some and other reason

(No like NIS server responding) and the search continues with the


5) Delete all the numbers in the file and the spaces at the beginning of the line.


Because there is no number in the original file, there is no space at the beginning of the line, here is a little bit of processing to generate a new test file:

[[Email protected] ~]# sed ' s/o/o7/;s/l/l4/;3,5s/^//' Nssw.txt > Nssw2.txt

[email protected] ~]# cat Nssw2.txt

An exampl4e Name Service Switch co7nfig file. This file should is

So7rted with the most-used services at the beginning.

#

. the entry ' [Notfound=return] ' means that's the search fo7r an

Entry sho7ul4d Stop if the search in the previous entry turned

Up no7thing. Note if the search fail4ed due to some and other reason

(L4ike No7 NIS Server responding) then the search continues with the


Take the Nssw2.txt file as an example, delete all numbers, the beginning of the line is the following:


1.[[email protected] ~]# sed-r ' s/[0-9]//g;s/^ () +//' Nssw2.txt

2.An example Name Service Switch config file. This file should is

3.sorted with the most-used services at the beginning.

4.#

5.the entry ' [Notfound=return] ' means that the search for an

6.entry should stop if the search in the previous entry turned

7.up nothing. Note If the search failed due to some and other reason

8.(like no NIS server responding) and the search continues with the


6) Add parentheses to each capital letter in the file.


Use "&" to invoke the entire lookup string in the S-replace operation, so refer to the following actions:

[[Email protected] ~]# sed ' s/[a-z]/(&)/g ' Nssw.txt

(A) n Example (n) Ame (s) ervice (s) witch config file. (T) His file should is

Sorted with the most-used services at the beginning.

#

(t) he entry ' [(n) (o) (T) (F) (o) (U) (n) (D) =return] ' means that the search for an

Entry should stop if the search in the previous entry turned

Up nothing. (N) Ote If the search failed due to some other reason

(like No (N) (I) (S) server responding) and the search continues with the


Or:

[Email protected] ~]# sed-r ' s/([A-z])/(\1)/g ' nssw.txt

(A) n Example (n) Ame (s) ervice (s) witch config file. (T) His file should is

Sorted with the most-used services at the beginning.

#

(t) he entry ' [(n) (o) (T) (F) (o) (U) (n) (D) =return] ' means that the search for an

Entry should stop if the search in the previous entry turned

Up nothing. (N) Ote If the search failed due to some other reason

(like No (N) (I) (S) server responding) and the search continues with the


The use of SED under Linux

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.