(mac) sed 删除包含A但是不包含B的行

z2xy
  • 38

例如:

aaa bbb
aaa ccc
aaa
bbb
hij

匹配后的结果是

aaa  bbb
hij

尝试过的

 sed -e '/\(aaa\)[^\(bbb\)]*$/d' test.txt

在手册http://www.gnu.org/software/sed/manual/sed.html上给出的

regexp1\|regexp2
Matches either regexp1 or regexp2. Use parentheses to use complex alternative regular expressions. The matching process tries each alternative in turn, from left to right, and the first one that succeeds is used. It is a GNU extension.

regexp1regexp2
Matches the concatenation of regexp1 and regexp2. Concatenation binds more tightly than \|, ^, and $, but less tightly than the other regular expression operators.

也看了这里的一些参考:
http://coolshell.cn/articles/9104.html

评论
阅读 4.4k
2 个回答
✓ 已被采纳

可是试试分解为三个步骤:

  1. 对于含有 B 的行,在行首或行尾放置特殊标记;

  2. 替换所有含有 aaa 但是行首或行尾不含有特殊标记的行;

  3. 去掉行首或行尾的特殊标记。

撰写回答

登录后参与交流、获取后续更新提醒

宣传栏