remove block of text between two lines based on content

前端 未结 3 1094
慢半拍i
慢半拍i 2020-12-20 05:01

I need to remove/filter a very large log file i managed to bring the log-file into blocks of text starting with a line containing <-- or -->

3条回答
  •  庸人自扰
    2020-12-20 05:27

    Try this:

    awk '/<--|-->/{rec=""; f=1} f{rec = rec $0 ORS} /Content-Length:/{ if (f && (rec !~ "REGISTER")) printf "%s",rec; f=0}' file
    

    If it doesn't do what you want, provide more info on what you want along with sample input and output.

    To break down the above, here's each statement on separate lines with some comments:

    awk '
       /<--|-->/ {rec=""; f=1} # find the start of the record, reset the string to hold it and set a flag to indicate we've started processing a record
       f {rec = rec $0 ORS}    # append to the end of the string containing the current record
       /Content-Length:/{      # find the end of the record
          if (f && (rec !~ "REGISTER")) # print the record if it doesn't contain "REGISTER"
             printf "%s",rec
          f=0                  # clear the "found record" indicator
       }
    ' file
    

    and if you have text between your records that you'd want printed, just add a test for the "found" flag not being set and invoke the default action of printing the current record (!f;)

    awk '/<--|-->/{rec=""; f=1} f{rec = rec $0 ORS} !f; /Content-Length:/{ if (f && (rec !~ "REGISTER")) printf "%s",rec; f=0}' file
    

提交回复
热议问题