0

如何替换文件中以aaa我不知道该行在哪里的文本开头的行。

文件.txt:

sadasd_dsada = (aa,bb,cc)
aaa = (aa,bb,cc)
sadasd_dsada = (aa,bb,cc)

代替:

aaa = (aa,bb,cc)

为了:

aaa = (dd,ee,ff)
4

1 回答 1

8

最简单的方法是用 进行替换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)

字符^$分别匹配正则表达式中的行首和行尾,这意味着替换只会发生在整行匹配中。

一旦您对更改感到满意,请使用-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)
于 2013-03-01T15:39:57.747 回答