如何替换以文本aaa
开头的文件中的行我不知道该行的位置。
files.txt:
sadasd_dsada = (aa,bb,cc)
aaa = (aa,bb,cc)
sadasd_dsada = (aa,bb,cc)
替换:
aaa = (aa,bb,cc)
对于:
aaa = (dd,ee,ff)
答案 0 :(得分:7)
最简单的方法是使用sed
进行替换:
$ sed 's/^aaa = (aa,bb,cc)$/aaa = (dd,ee,ff)/' file
sadasd_dsada = (aa,bb,cc)
aaa = (dd,ee,ff)
sadasd_dsada = (aa,bb,cc)
字符^
和$
分别在regexp中匹配行的开头和结尾,这意味着只有整行匹配才会发生替换。
如果您对更改感到满意,请使用-i
选项保存回文件。
$ sed -i 's/^aaa = (aa,bb,cc)$/aaa = (dd,ee,ff)/' file
修改强>
$ cat file
sadasd_dsada = (aa,bb,cc)
aaa = (aa,bb,cc)
aaa = foo
sadasd_dsada = (aa,bb,cc)
aaa = bar
$ sed 's/^aaa = .*/aaa = (dd,ee,ff)/' file
sadasd_dsada = (aa,bb,cc)
aaa = (dd,ee,ff)
aaa = (dd,ee,ff)
sadasd_dsada = (aa,bb,cc)
aaa = (dd,ee,ff)