我知道如何向文件添加新文本,但如何編輯它?
範例:hello_world = 1
使用以下命令新增至 test.txt:
echo "hello_world = 1" >> test.txt
但我怎麼能改變1
或0
其他什麼?
答案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