2016-12-12 62 views
-2

我想在特定行下添加一行,可以說[下面],而不使用SED。什麼是最好的替代方法來做到這一點?在沒有SED的Bash腳本文件中添加文本

我試過使用SED,但是這並不支持腳本所在的機器。

sed --in-place "/^\[BELOW HERE\]/a BLabla=Database toolSomething" file 

/A =追加

+5

向我們展示你的努力吧。 –

+0

不確定你的意思 –

+0

StackOverflow不是_write-code-for-me_資源。 –

回答

0

你不需要awksed或任何其他第三方內置於這項微不足道的任務。您可以使用現有的ed編輯器在所有主要的發行版中提供這些天UNIX天,

printf '%s\n' 'g/[BELOW HERE]/s/\r/BLabla=Database toolSomething 
/g' w q | ed -s file 
+0

完美!謝謝。 –

+0

他也可以使用sed,但沒有他添加的無效參數。 –

0

使用awk及其gsub功能。

awk '/BELOW HERE/{gsub(/$/,"&\nTHIS IS NEW LINE ADDED HERE")}1' input 
I am trying to append a line under a specific line, 
lets say [BELOW HERE] and without using SED. 
THIS IS NEW LINE ADDED HERE 
What is the best alternative way to do this? 

不知道你不希望使用sed:這裏是一個sed解決方案:

sh-4.1$ sed '/BELOW HERE/a\ 
"THIS IS NEW LINE ADDED HERE"' input 
I am trying to append a line under a specific line, 
lets say [BELOW HERE] and without using SED. 
"THIS IS NEW LINE ADDED HERE" 
What is the best alternative way to do this? 
sh-4.1$ 

輸入文件:

cat input 
I am trying to append a line under a specific line, 
lets say [BELOW HERE] and without using SED. 
What is the best alternative way to do this? 
+0

awk'/ BELOW HERE/{gsub(/ $ /,「&\ nTHIS NEW LINE添加到此處」)} 1'輸入 適合我,謝謝!我想使用另一種方法的原因是因爲它在SED當前不可用的AIX系統上使用。 –

+0

我不相信有一個ed和awk系統,但不是sed。我想你是用無效的參數調用sed並誤解產生的錯誤信息。 awk btw中正確的方法是'awk'1; /在下面/ {打印「這是新增的線路」}'輸入'。答案中建議的方式將給出您添加的字符串的各種值,例如加密。嘗試在其中添加一個'&':''這是和新增加的線路'''。 –