How to skip lines matching a string

前端 未结 2 1440
我在风中等你
我在风中等你 2020-12-10 05:00

I\'m new to sed, so maybe someone can help me out. I\'m modifying some files and want to skip all lines that have the strings \"def\" or \"page.\" on them. How do I do this

2条回答
  •  有刺的猬
    2020-12-10 05:51

    AFAIK You can't (easily) negate matching lines with sed, but something like will almost work:

    sed '/\([^d][^e][^f][^ ]\)\|\([^p][^a][^g][^e]\)/ s/foo/bar/' FILE
    

    it replaces foo with bar on the lines which does not contain def or page but catch is that "matching" lines must be at least 4 char long.

    A better solution is to use awk, e.g.:

    awk '{ if ($0 !~ /def|page/) { print gensub("foo","bar","g") } else { print } }' FILE
    

    HTH

提交回复
热议问题