2012-04-10 160 views
1

我有一個包含替換文本

<this can be anything, any number of line - no hardcoded> 
<this can be anything, any number of line - no hardcoded> 
    param1=<this can be any value - no hardcoded> 
<this can be anything, any number of line - no hardcoded> 
<this can be anything, any number of line - no hardcoded> 

text1.txt我想更換這部分這可以是任何值 - 沒有硬編碼ABC

應該是什麼是正確的SED命令? 請注意,由於環境限制,我不能靈活地使用任何語言,如perl,python或ruby〜,只能使用Unix命令。 除了之外沒有硬編碼參數1

非常感謝!

+0

[這太問題(http://stackoverflow.com/questions/1074450/sed替換部分)可能會有所幫助。 – 2012-04-10 08:25:17

+0

評論中的所有樣本值如何? – 2012-04-10 08:34:11

+0

@shiplu你能否澄清一下構成註釋的內容,按照OP的問題'text1.txt'是一個普通的文本文件,所以任何東西都可以表示註釋? – beny23 2012-04-10 08:37:30

回答

1

你想要的調用是

sed -e 's/param1=.*/param1=abc/' text1.txt 
+0

這可能會更改評論中的所有示例值。沒有? – 2012-04-10 08:33:53

+0

如果你的意思是六個字符的序列'param1 ='將始終使用'abc'填充剩餘的字符,即使它出現在「註釋」中,那麼是的。但OP從未對評論發表任何評論。 – 2012-04-10 08:36:26

+0

非常感謝Ray Toal – iwan 2012-04-12 07:40:00

1

這聽起來像你想要的東西像s/<.\+>$/<abc>/

$ cat test1.txt 

<this can be anything, any number of line - no hardcoded> 
<this can be anything, any number of line - no hardcoded> 
    param1=<this can be any value - no hardcoded> 
<this can be anything, any number of line - no hardcoded> 
<this can be anything, any number of line - no hardcoded> 

$ sed 's/<.\+>$/<abc>/' test1.txt 

<abc> 
<abc> 
    param1=<abc> 
<abc> 
<abc>