Sed : print all lines after match

前端 未结 5 1440
-上瘾入骨i
-上瘾入骨i 2020-12-14 10:46

I got my research result after using sed :

zcat file* | sed -e \'s/.*text=\\(.*\\)status=[^/]*/\\1/\' | cut -f 1 - | grep \"pattern\"

相关标签:
5条回答
  • 2020-12-14 11:14

    Printing all lines after a match in sed:

    $ sed -ne '/pattern/,$ p'
      # alternatively, if you don't want to print the match:
    $ sed -e '1,/pattern/ d'
    

    Filtering lines when pattern matches between "text=" and "status=" can be done with a simple grep, no need for sed and cut:

    $ grep 'text=.*pattern.* status='
    
    0 讨论(0)
  • 2020-12-14 11:20

    alternatively you can use awk

    awk '/pattern/,EOF'
    

    perhaps can combine with all the previous operations in awk as well.

    0 讨论(0)
  • 2020-12-14 11:21
    zcat file* | sed -e 's/.*text=\(.*\)status=[^/]*/\1/' | ***cut -f 1 - | grep "pattern"***
    

    instead change the last 2 segments of your pipeline so that:

    zcat file* | sed -e 's/.*text=\(.*\)status=[^/]*/\1/' | **awk '$1 ~ "pattern" {print $0}'**
    
    0 讨论(0)
  • 2020-12-14 11:23

    The seldom used branch command will do this for you. Until you match, use n for next then branch to beginning. After match, use n to skip the matching line, then a loop copying the remaining lines.

    cat file | sed -n -e ':start; /pattern/b match;n; b start; :match n; :copy; p; n ; b copy'
    
    0 讨论(0)
  • 2020-12-14 11:27

    Maybe this is what you actually want? Find lines matching "pattern" and extract the field after text= up through just before status=?

    zcat file* | sed -e '/pattern/s/.*text=\(.*\)status=[^/]*/\1/'
    

    You are not revealing what pattern actually is -- if it's a variable, you cannot use single quotes around it.

    Notice that \(.*\)status=[^/]* would match up through survstatus=new in your example. That is probably not what you want? There doesn't seem to be a status= followed by a slash anywhere -- you really should explain in more detail what you are actually trying to accomplish.

    Your question title says "all line after a match" so perhaps you want everything after text=? Then that's simply

    sed 's/.*text=//'
    

    i.e. replace up through text= with nothing, and keep the rest. (I trust you can figure out how to change the surrounding script into zcat file* | sed '/pattern/s/.*text=//' ... oops, maybe my trust failed.)

    0 讨论(0)
提交回复
热议问题