我想在Linux中的特定行號處添加一個字符串到文件。我搜查,發現像這樣的命令:將字符串添加到特定行號的文件
sed "5i helloworld" test.txt
在5號線加helloworld
,但我得到了一個錯誤:
sed: command garbled.
似乎對sed的Solaris不支持選項-i
。我在這裏測試RedHat。有沒有其他的命令可以在這裏使用?任何其他方式?
我想在Linux中的特定行號處添加一個字符串到文件。我搜查,發現像這樣的命令:將字符串添加到特定行號的文件
sed "5i helloworld" test.txt
在5號線加helloworld
,但我得到了一個錯誤:
sed: command garbled.
似乎對sed的Solaris不支持選項-i
。我在這裏測試RedHat。有沒有其他的命令可以在這裏使用?任何其他方式?
如何鍵入命令如i
,a
和c
,舊版seds有點挑剔。嘗試實際續行:
sed '5i\
helloworld' test.txt
的i text
語法是GNU擴展。 POSIX sed只知道帶有換行符的i\
版本。
另外,注意有和-i
選項(就地編輯)關於sed i
命令 (插入文本)之間的差。
或 「功能」。
_可選reading_:總結術語對話:[_POSIX_ spec](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/sed.html)和[_BSD_ Sed man page](https:/ /www.freebsd.org/cgi/man.cgi?query=sed&sektion=&n=1)調用腳本中可用的功能單元 - 例如'i' - 一個_function_和一個_instance of calling_這樣的單元(名稱函數加上它的參數)_command_。相比之下,[_GNU_ Sed的手冊頁](https://www.gnu.org/software/sed/manual/sed.html#Common-Commands)並未作出此區分,而_only_使用術語_command_。 – mklement0
感謝您的意見。現在它適用於我。 – Richard
這裏是awk
解決方案:
awk 'NR==5{1;print "Hey there this is new text added on line 5"}1' inputfile
您正在使用Linux或Solaris? – miken32
@ miken32 linux在這裏。 – Richard
那麼你會有什麼工作,你爲什麼提到Solaris? – miken32