在沒有開啟編輯器的情況下新增/編輯文件中的行文字(linux命令)

在沒有開啟編輯器的情況下新增/編輯文件中的行文字(linux命令)

我知道如何向文件添加新文本,但如何編輯它?

範例:hello_world = 1使用以下命令新增至 test.txt:

echo "hello_world = 1" >> test.txt

但我怎麼能改變10其他什麼?

答案1

使用sed

sed -i 's/1/0/g' test.txt

一般來說:

sed -i 's/oldstring/newstring/g' filename

請參閱man sed以獲取更多資訊。

答案2

透過awk,

awk '{sub(/1/,"0")}1' infile > outfile

例子:

$ echo 'hello_world = 1' | awk '{sub(/1/,"0")}1'
hello_world = 0

相關內容