我是sed的新手,所以也许有人可以帮助我.我正在修改一些文件,并希望跳过所有包含字符串"def"或"page"的行.在他们.我怎么在sed这样做?
bra*_*zzi 22
如果我理解得很好,你想对除了匹配正则表达式的一些线以外的各种线应用一些更改,对吧?在这种情况下,让我们假设我有以下文件:
$ cat file
this is a def
this has no d e f
this is a page by the way
but this is no p a g e as we know ito
Run Code Online (Sandbox Code Playgroud)
我们想要替换所有this,that但忽略包含def或的行page.所以首先我们删除以defor 开头的行page:
/def/d;/page/d;
Run Code Online (Sandbox Code Playgroud)
然后我们照常应用我们的操作:
s/this/that/g
Run Code Online (Sandbox Code Playgroud)
结果是:
$ sed '/def/d;/page/d;s/this/that/g' file
that has no d e f
but that is no p a g e as we know ito
Run Code Online (Sandbox Code Playgroud)
但如果通过"跳过"表示"不要应用我的操作",只需取消地址:
$ sed -E '/(def|page)/!s/this/that/g' file
this is a def
that has no d e f
this is a page by the way
but that is no p a g e as we know ito
Run Code Online (Sandbox Code Playgroud)
以上陈述正确.有趣的是,'或'运算符与"扩展正则表达式"相关联.因此,您必须为"扩展正则表达式"指定-E,因为默认情况下,sed仅使用"基本正则表达式".
例如,以下语句不起作用:
$ sed -e '/(def|page)/!s/[A-Za-z_]*login[A-Za-z_]*/page.&/g' < file > new_file
Run Code Online (Sandbox Code Playgroud)
但是下面的陈述有效:
$ sed -E '/(def|page)/!s/[A-Za-z_]*login[A-Za-z_]*/page.&/g' < file > new_file
Run Code Online (Sandbox Code Playgroud)