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

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

例如:

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


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

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

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

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


请参考 Sed regex and substring negation
mac 上执行:

sed -e '/bbb/b' -e '/aaa/d' test.txt

输出为:

aaa bbb
bbb
hij
【热门文章】
【热门文章】