简体   繁体   中英

sed / awk: Delete all lines matching a word not followed by another word

I have the following lines in my file.

<stuff>
test1.*test2
test1
test1.*test2
test1
<other stuff>

I want to delete all lines containing test1, but not followed by test2 -- which means I should end up with:

<stuff>
test1.*test2
test1.*test2
<other stuff>

I've tried a lot of regular expressions, but can't seem to crack either sed or awk. This was my latest attempt, after testing with vim regexp.

sed -i .bk '/^.*test1\(\(test2\)\@!.\)*$/d' file

(This is part of a bash script on Mac OS X)

Use two expressions. The first one skips the cases where test1 is followed by test2 , the second one removes test1 - it can only be reached if test2 wasn't there.

sed -e '/test1.*test2/b' -e '/test1/d'

Try something like:

awk '/test1/&&!/test2/{next}1' file

We tell awk to:

  • Look for lines with test1 .
  • On the same line test2 should not be present
  • If such line is found we skip
  • If such line is not found we print

$ cat file
<stuff>
test1.*test2
test1
test1.*test2
test1
<other stuff>

$ awk '/test1/&&!/test2/{next}1' file
<stuff>
test1.*test2
test1.*test2
<other stuff>

带有perl正则表达式的GNU grep:

grep -vP 'test1(?!.*test2)' file
$ awk '!/test1/||/test2/' file
<stuff>
test1.*test2
test1.*test2
<other stuff>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM