2012-05-01 38 views
1

我有以下SED/awk中如何添加到匹配的字符串

<property>PASSWORD_X_1</property> 
<property>PASSWORD_A_2</property> 
<property>PASSWORD_B_6</property> 

如此類推提到它包含了文本文件..

現在我期待的行更改爲:

<property>{PASSWORD_X_1}</property> 
<property>{PASSWORD_A_2}</property> 
<property>{PASSWORD_B_6}</property> 

我可以改變初始 「{」 使用簡單的sed表達式:

sed -e '/PASSWORD/{PASSWORD/g' 

無法弄清楚如何追加「}」字符。

任何建議..?

Ĵ

回答

2

sed -e 's/PASSWORD_[A-Z]_[0-9]/{&}/'

容易修改!

+0

就像一個魅力!不知道'&'表示sed中當前匹配的字符串。非常感謝dav1d。 – joesatch

2

這應該工作:

awk -F"<|>" '{ print "<" $2 ">{" $3 "}<" $4 ">" }' file.text

+0

如果密碼中有一個< or >字符,則會崩潰。 – Chisholm

+0

謝謝你回答Chisholm!其中一個基於sed的解決方案適合我。 – joesatch

0

這可能會爲你工作:

sed 's/PASSWORD[^<]*/{&}/' file 
0
awk '{sub(">P",">{P") sub("</","}</");}1' file 
相關問題