linux下sed的使用

本篇主要講解:c#

---sed文本塊的處理bash



1、sed文本塊的處理測試


1.sed文本塊處理的基本用法spa


 經常使用的處理選項有:i 行前插入文本server

  a  行後插入文本ip

  c  替換當前行文檔

 須要插入多行文本內容時,一種方法是以「\n」表示換行,另外一種是以「\」強制分隔。後面這種方法可能更符合閱讀習慣。字符串


 使用「&」可調用s替換操做中的整個查找串it


仍是用測試文檔:rclocal.txtio

[root@svr5 ~]# cat rclocal.txt

1 #!/bin/sh

2 #

3 # This script will be executed *after* all the other init scripts.

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

5 # want to do the full Sys V style init stuff.

6

7 touch /var/lock/subsys/local


1)行前插入文本 i

在第3行以前插入一行「Insert before」字符串:

[root@svr5 ~]# sed '3iInsert before' rclocal.txt

1 #!/bin/sh

2 #

Insert before

3 # This script will be executed *after* all the other init scripts.

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

5 # want to do the full Sys V style init stuff.

6

7 touch /var/lock/subsys/local

在最後一行以前插入一行「Insert before」字符串:

[root@svr5 ~]# sed '$iInsert before' rclocal.txt

1 #!/bin/sh

2 #

3 # This script will be executed *after* all the other init scripts.

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

5 # want to do the full Sys V style init stuff.

6

Insert before

7 touch /var/lock/subsys/local

在包含「init」的每一行以前插入一行「xxxx」字符串:

[root@svr5 ~]# sed '/init/ixxxx' rclocal.txt

1 #!/bin/sh

2 #

.xxxx

3 # This script will be executed *after* all the other init scripts.

xxxx

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

xxxx

5 # want to do the full Sys V style init stuff.

6

7 touch /var/lock/subsys/local

2)行後插入文本 a

在第3行以後插入一行「Insert after」字符串:

[root@svr5 ~]# sed '3aInsert after' rclocal.txt

1 #!/bin/sh

2 #

3 # This script will be executed *after* all the other init scripts.

Insert after

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

5 # want to do the full Sys V style init stuff.

6

7 touch /var/lock/subsys/local

在最後一行以後追加一行「Insert after」字符串:

[root@svr5 ~]# sed '$aInsert after' rclocal.txt

1 #!/bin/sh

2 #

3 # This script will be executed *after* all the other init scripts.

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

5 # want to do the full Sys V style init stuff.

6

7 touch /var/lock/subsys/local

Insert after

在包含「stuff」的每一行以後插入一行「xxxx」字符串:

[root@svr5 ~]# sed '/stuff/axxxx' rclocal.txt

1 #!/bin/sh

2 #

3 # This script will be executed *after* all the other init scripts.

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

xxxx

5 # want to do the full Sys V style init stuff.

xxxx

6

7 touch /var/lock/subsys/local


3)替換當前行 c

將第1行整行替換爲「#!/bin/bash」:

[root@svr5 ~]# sed '1c#!/bin/bash' rclocal.txt

#!/bin/bash

2 #

3 # This script will be executed *after* all the other init scripts.

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

5 # want to do the full Sys V style init stuff.

6

7 touch /var/lock/subsys/local

將第1~4行總體替換爲「#!/bin/bash」:

[root@svr5 ~]# sed '1,4c#!/bin/bash' rclocal.txt

#!/bin/bash

5 # want to do the full Sys V style init stuff.

6

7 touch /var/lock/subsys/local

將包含「/bin/sh」的每一行分別替換爲「#!/bin/bash」:

[root@svr5 ~]# sed '/\/bin\/sh/c#!/bin/bash' rclocal.txt

#!/bin/bash

2 #

3 # This script will be executed *after* all the other init scripts.

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

5 # want to do the full Sys V style init stuff.

6

7 touch /var/lock/subsys/local


將包含單詞「init」的每一行分別替換爲「#!/bin/bash」:

[root@svr5 ~]# sed '/\<init\>/c#!/bin/bash' rclocal.txt

1 #!/bin/sh

2 #

#!/bin/bash

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

#!/bin/bash

6

7 touch /var/lock/subsys/local

************注:\<表示已某單詞開頭、,  \>表示已某單詞結尾 \<init\>就表示整個單詞,像initital是不符合檢索結果的*********************************


4)多行文本的處理


須要插入多行文本內容時,一種方法是以「\n」表示換行,另外一種是以「\」強制分隔。後面這種方法可能更符合閱讀習慣。以在第3行以前插入三行文本,內容依次爲「xxxx」、「yyyy」、「zzzz」爲例,下面的操做能夠對比兩種方法的效果:


[root@svr5 ~]# sed '3ixxxx\nyyyy\nzzzz' rclocal.txt         //方法1

1 #!/bin/sh

2 #

xxxx

yyyy

zzzz

3 # This script will be executed *after* all the other init scripts.

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

5 # want to do the full Sys V style init stuff.

6

7 touch /var/lock/subsys/local

[root@svr5 ~]# sed '3ixxxx\

> yyyy\

> zzzz' rclocal.txt                                         //方法2

1 #!/bin/sh

2 #

xxxx

yyyy

zzzz

3 # This script will be executed *after* all the other init scripts.

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

5 # want to do the full Sys V style init stuff.

6

7 touch /var/lock/subsys/local

2.利用sed文本塊處理調整系統配置


1)修改主機名


主機名的配置文件位於/etc/sysconfig/network,主機名設置以「HOSTNAME」打頭。

修改前:

[root@svr5 ~]# cat /etc/sysconfig/network

NETWORKING=yes

NETWORKING_IPV6=no

HOSTNAME=svr5.tarena.com

將以「HOSTNAME」開頭的行整行替換,設爲「HOSTNAME=mysvr.example.org」:

[root@svr5 ~]# sed -i '/^HOSTNAME/cHOSTNAME=mysvr.example.org' /etc/sysconfig/network

確認替換結果:

[root@svr5 ~]# cat /etc/sysconfig/network NETWORKING=yes

NETWORKING_IPV6=no

HOSTNAME=mysvr.example.org

如下操做能夠恢復原狀:

[root@svr5 ~]# sed -i '/^HOSTNAME/cHOSTNAME=svr5.tarena.com' /etc/sysconfig/network

[root@svr5 ~]# cat /etc/sysconfig/network NETWORKING=yes

NETWORKING_IPV6=no

HOSTNAME=svr5.tarena.com

2)添加hosts主機映射記錄

在/etc/hosts文件最後一行後添加任務要求的2條映射記錄:

[root@svr5 ~]# sed -i '$a192.168.4.5 svr5.tarena.com svr5\

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

驗證添加效果:

[root@svr5 ~]# tail -2 /etc/hosts

192.168.4.5 svr5.tarena.com svr5

119.75.217.56 www.baidu.com



2.sed文本處理練習

先創建一個包含英文段落的測試文件,好比可以使用/etc/nsswitch.conf文件。爲了方便查看效果,咱們將從這個文件中取第4~10行,並去掉開頭的「# 」。開頭的10行內容以下所示:


[root@svr5 ~]# head -10 /etc/nsswitch.conf

#

# /etc/nsswitch.conf

#

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

# 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 that if the search failed due to some other reason

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


截取操做及結果以下所示:

[root@svr5 ~]# sed -n '4,10p' /etc/nsswitch.conf | sed 's/# //' > nssw.txt

[root@svr5 ~]# cat nssw.txt

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

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 that if the search failed due to some other reason

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

本小節的操做即便用nssw.txt做爲測試文件。

1)刪除文件中每行的第二個、最後一個字符。

分兩次替換操做,第一次替換掉第2個字符,第二次替換掉最後一個字符:

[root@svr5 ~]# 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 that if the search failed due to some other reaso

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



2)刪除文件中每行的第二個、最後一個單詞。


分兩次替換操做,第一次替換掉第2個單詞,第二次替換掉最後一個單詞:

[root@svr5 ~]# 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 the search for

entry stop if the search in the previous entry

up . Note that if the search failed due to some other

(like NIS server responding) then the search continues with

3)將文件中每行的第一個、第二個字符互換。

每行文本拆分爲「第1個字符」、「第2個字符」、「剩下的全部字符」三個部分,而後經過替換操做重排順序爲「2-1-3」:

[root@svr5 ~]# sed -r 's/^(.)(.)(.*)/\2\1\3/' nssw.txt

nA example Name Service Switch config file. This file should be

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 that if the search failed due to some other reason

l(ike n up . Note that if the search failed due to some other

(like NIS server responding) then the search continues with


4)將文件中每行的第一個、第二個單詞互換。


每行文本拆分爲「第1個單詞」、「單詞分隔」、「第2個單詞」、「剩下的全部字符」四個部分,而後經過替換操做重排順序爲「3-2-1-4」:


[root@svr5 ~]# sed -r 's/([a-Z]+)([^a-Z]*)([a-z]+)(.*)/\3\2\1\4/' nssw.txt example An Name Service Switch config file. This file should be

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 that if the search failed due to some other reason

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


5)刪除文件中全部的數字、行首的空格。


因原文件內沒有數字,行首也沒有空格,這裏稍做一點處理,生成一個新測試文件:

[root@svr5 ~]# sed 's/o/o7/;s/l/l4/;3,5s/^/ /' nssw.txt > nssw2.txt

[root@svr5 ~]# cat nssw2.txt

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

so7rted with the most-used services at the beginning.

#

.The entry '[NOTFOUND=return]' means that the search fo7r an

entry sho7ul4d stop if the search in the previous entry turned

up no7thing. Note that if the search fail4ed due to some other reason

(l4ike no7 NIS server responding) then the search continues with the


以nssw2.txt文件爲例,刪除全部數字、行首空格的操做以下:


1.[root@svr5 ~]# sed -r 's/[0-9]//g;s/^( )+//' nssw2.txt

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

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 that if the search failed due to some other reason

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


6)爲文件中每一個大寫字母添加括號。


使用「&」可調用s替換操做中的整個查找串,因此可參考下列操做解決:

[root@svr5 ~]# sed 's/[A-Z]/(&)/g' nssw.txt

(A)n example (N)ame (S)ervice (S)witch config file. (T)his file should be

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 that if the search failed due to some other reason

(like no (N)(I)(S) server responding) then the search continues with the


或者:

[root@svr5 ~]# sed -r 's/([A-Z])/(\1)/g' nssw.txt

(A)n example (N)ame (S)ervice (S)witch config file. (T)his file should be

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 that if the search failed due to some other reason

(like no (N)(I)(S) server responding) then the search continues with the

相關文章
相關標籤/搜索