2017-02-27 24 views
0

美好的一天。奇怪的字符做改變後使用perl命令

讓說我的文件內容如下::

java ${AGENT_JAVAOPTS} -Xmx2560m com.xxx.xxx.xxx.AgentXX -inifile ${XXX_AGENT_INI} -queues winall,dustat,envstat,netstat,iostat,winconfig,netwarestat,netwareconfig,pawmin,paw15,db2,sqlserver,vmstatvmw2,vmstatvm2,netstatvm,netstatvmw,vmstatvm,vmstatvmw,iostatvm,iostatvmw,envstatvm,envstatvmw,vmscpu,vmsdisk,vmsmem,vmstatvcw,process,winprocess -log xxx0202.'$DotDate'.log -AgentName xxx0202 

於是,我試圖改變字符串後 '-queue' 爲 「荃銀,工藝,winconfig,vmscpu,vmstat的,dustat」,這是我在UNIX使用以下命令::

perl -pi -e 'tr/winall,dustat,envstat,netstat,iostat,winconfig,netwarestat,netwareconfig,pawmin,paw15,db2,sqlserver,vmstatvmw2,vmstatvm2,netstatvm,netstatvmw,vmstatvm,vmstatvmw,iostatvm,iostatvmw,envstatvm,envstatvmw,vmscpu,vmsdisk,vmsmem,vmstatvcw,process,winprocess/winall,process,winconfig,vmscpu,vmstat,dustat/' file 

然而,它產生一個奇怪的字符的輸出如下::

jaia ${AGENT_JAVAOPTS} -Xtxtt60t u,t.itt.ott.cotau,.At,ncDP -iniail, ${SRM_AGENT_INI} -tr,r,o winall,procac,,niocac,n,cocac,i,ocac,winu,nait,n,cwat,ocac,n,cwat,u,nait,tawtin,tawtt,ptt,otlo,ti,t,itocacitwt,itocacitt,n,cocacit,n,cocacitw,itocacit,itocacitw,i,ocacit,i,ocacitw,,niocacit,,niocacitw,itoutr,itopiot,itot,t,itocaciuw,tt,u,oo,wintt,u,oo -l,t tlp0t0t.'$D,cDac,'.l,t -At,ncNat, tlp0t0t 

它如何產生像上面那樣的輸出?

輸出假設是因爲:

java ${AGENT_JAVAOPTS} -Xmx2560m com.xxx.xxx.xxx.AgentXX -inifile ${XXX_AGENT_INI} -queues winall,process,winconfig,vmscpu,vmstat,dustat -log xxx0202.'$DotDate'.log -AgentName xxx0202 

需要幫助解決該問題。

+1

如果以下任何答案對您的目標有所幫助,請通過在答案旁邊勾選正確的符號來接受/提出封閉答案。 –

回答

2

tr///是音譯,而不是替代。

它將REPLACEMENTLIST的第一個字符替換爲SEARCHLIST的第一個字符(類似於sed中的y///),依此類推。你需要的是替代s///。有關詳細信息,請參閱perlop

+0

謝謝@choroba,它有助於=) – MrAZ

1
sed -r 's/(^.*-queues)(.*)/\1 winall,process,winconfig,vmscpu,vmstat,dustat/' inputfile 
java ${AGENT_JAVAOPTS} -Xmx2560m com.xxx.xxx.xxx.AgentXX -inifile ${XXX_AGENT_INI} -queues winall,process,winconfig,vmscpu,vmstat,dustat 

這裏,線分爲使用backrefrencing 2塊,第一個是從開始到-queues和第二點是從該點才結束。

或者您可以使用awk將文本存儲到變量中並在更換時使用它。在這裏,您可以通過更改變量var的值來更改文本。

awk -v FS='-queues' -v var='winall,process,winconfig,vmscpu,vmstat,dustat' '{print $1 FS var}' inputfile 
+0

感謝這個sed命令,它給了我一個想法......但是之後的字符串是-log xxx0202。'$ DotDate'.log -AgentName xxx0202在運行此命令後消失。 – MrAZ

+0

@Azizul,在我只提供了部分輸入的命令中,您需要根據需要更新'\ 1'後面的文本。 –

+0

謝謝,它有幫助=) – MrAZ