我有一个名为“ test-file”的文件。它只有两行(第一行的前面带有#号,第二行的内容相同但没有哈希):
#contents of line
contents of line
我想使用sed命令更新/替换第二行的内容。我知道它的简单语法,例如:
sed -i 's/.*contents of line.*/updated contents/' test-file
但是上面的命令替换了这两行,而我不想用前面的#号更新第一行。
尝试
sed 's/^[^#]*contents of line.*/updated contents/' test-file
或以line anchor开头^
,并且您知道该行以开头content ...
,则可以执行以下操作:
sed 's/^contents of line.*/updated contents/' test-file
如果您知道要更新的行,则可以直接使用其地址行号进行更改,例如。
sed '2s/.*/updated content/' test-file
-i
当您对结果满意时添加。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句