1. 程式人生 > >由一個實例淺析sed用法

由一個實例淺析sed用法

正則 表達式 sed

首先,假設我們有一個文件,叫123.txt,#cat一下看到裏面的內容是這樣的:

[[email protected] ~]# cat 123.txt 
james
curry
  durant
wade
yaoming
     messi
[[email protected] ~]#


如果我們想在james前面加上lebron,那麽采用的sed語句就是:#sed -i ‘/^james/s/^/lebron /‘ 123.txt,如果要在curry後面加上 champion,那麽采用的語句就是:#sed -i ‘/^curry/s/$/ champion!/‘ 123.txt


使用完上面兩句話之後,再#cat

一下,看下效果:

[[email protected] ~]# cat 123.txt 
lebron james
curry champion!
  durant
wade
yaoming
     messi
[[email protected] ~]#


現在我們要把 durant前面加上FMVP這幾個字母,按照上面的語句找葫蘆畫瓢的話,應該是:#sed -i ‘/^durant/s/^/FMVP /‘ 123.txt但是很抱歉,這個語句是錯誤的!因為^是匹配開頭durant的意思,而我們再看一下durant那一行的開頭是空格。


那麽就要用liunx的正則來匹配空格,於是這句話就變成了:#sed -i ‘/^\s\+durant/s/^/FMVP/‘ 123.txt

,^\s\+這個就是正則裏匹配空格的意思 。


#cat一下:

[[email protected] ~]# cat 123.txt 
lebron james
curry champion!
FMVP   durant
wade
yaoming
     messi
[[email protected] ~]#


那麽現在要在messi後面加上"GOAL !!!",就很簡單了。語句是:#sed -i ‘/^\s\+messi/s/$/ GOAL !!!/‘ 123.txt


以上我們把有/無空格情況下的首尾添加字符都練習了一遍,下面我們要看看如果要在中間添加怎麽辦?


比如說,有一天苦逼的運維接到開發PL的郵件,說”由於安全基線要求,現在需要監聽內網端口“,具體的需求就是把所有含tomcat的模塊裏的server.xml的文件裏添加上內網IP。


原有的server.xml的節選如下:

    <Service name="LMS">
<Connector port="8080" connectionTimeout="20000" protocol="org.apache.coyote.http11.Http11NioProtocol" redirectPort="8443" enableLookups="false" disableUploadTimeout="true" maxThreads="500" minSpareThreads="20" acceptCount="100"/>

<Connector port="8088" connectionTimeout="20000" protocol="org.apache.coyote.http11.Http11NioProtocol" redirectPort="8443" enableLookups="false" disableUploadTimeout="true" maxThreads="500" minSpareThreads="20" acceptCount="100"/>

<Connector port="8099" protocol="AJP/1.3" redirectPort="8443" />
<Engine defaultHost="localhost" name="Catalina">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase" />
</Realm>

現在要把<Connector port="8099" protocol="AJP/1.3" redirectPort="8443" /> 這一句裏面加上內網IP,改成這樣:

<Service name="LMS">
<Connector port="8080" connectionTimeout="20000" protocol="org.apache.coyote.http11.Http11NioProtocol" redirectPort="8443" enableLookups="false" disableUploadTimeout="true" maxThreads="500" minSpareThreads="20" acceptCount="100"/>

<Connector port="8088" connectionTimeout="20000" protocol="org.apache.coyote.http11.Http11NioProtocol" redirectPort="8443" enableLookups="false" disableUploadTimeout="true" maxThreads="500" minSpareThreads="20" acceptCount="100"/>

<Connector port="8099" address="1.2.3.4" protocol="AJP/1.3" redirectPort="8443" />
<Engine defaultHost="localhost" name="Catalina">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase" />
</Realm>

請問怎麽做?

答案1:

sed -i ‘/<Connector port="8099"/s/port="8099"/port="8099" address="1.2.3.4"/g‘ server.xml

答案2:

sed -i [email protected] port="8099"@& address="1.2.3.4"@‘ server.xml



本文出自 “生活就是等待戈多” 博客,請務必保留此出處http://chenx1242.blog.51cto.com/10430133/1939938

由一個實例淺析sed用法