In sed or awk, how do I handle record separators that * can * span multiple lines?

My log file:

 Wed Nov 12 blah blah blah blah cat1
 Wed Nov 12 blah blah blah blah
 Wed Nov 12 blah blah blah blah 
 Wed Nov 12 blah blah blah blah cat2
     more blah blah
     even more blah blah
 Wed Nov 12 blah blah blah blah cat3
 Wed Nov 12 blah blah blah blah cat4

I want to parse all multi-line entries where cat is found in the first line. What is the best way to do this in sedand / or awk?

i.e. I want my parsing to create:

 Wed Nov 12 blah blah blah blah cat1
 Wed Nov 12 blah blah blah blah cat2
     more blah blah
     even more blah blah
 Wed Nov 12 blah blah blah blah cat3
 Wed Nov 12 blah blah blah blah cat4
+3
source share
4 answers

if you say that each line starting with a space is a continuation of the description of its simple with (g) awk (this is from my memory, so it may contain some minor typos and for better readability with some additional line breaks):

awk " BEGIN { multiline = 0;} 
      ! /^ / { if (whatever) 
                 { print; multiline = 1;} 
               else 
                 multiline = 0; 
             } 
        /^ / {if (multiline == 1) 
                 print;
             } 
     " 
      yourfile

where whateveris your check if your exit should happen (for example, for a cat).

+1

, '\01' '\02', , :

c1=`echo -en '\01'`
c2=`echo -en '\02'`
cat logfile | tr '\n' $c1 | sed "s/$c1    /$c2/g" | sed "s/$c1/\n/g" | grep cat | sed "s/$c2/\n    /g"

: ASCII 1 ( , ), " ---" ASCII 2 ( ). ASCII 1 , , ASCII 2. grepped cat, ASCII 2 ---.

+1

Something like that?

awk 'function print_part() { if(cat) print part }  /^  / { part = part "\n" $0; next } /cat[0-9]$/ { print_part(); part = $0; cat = 1; next;  } { print_part(); cat=0} END { print_part() }' inputfile

/^ / regexp identifies continuation lines.

/cat[0-9]$/ regexp identifies the start lines that you want to keep.

0
source

Another approach would be to establish RSas something different from the usual \n. For instance:

$ awk -v Pre=Wed 'BEGIN {RS = "\\n?\\s*" Pre} /cat.\n?/ {print Pre $0}' file.log
Wed Nov 12 blah blah blah blah cat1
Wed Nov 12 blah blah blah blah cat2
     more blah blah
     even more blah blah
Wed Nov 12 blah blah blah blah cat3
Wed Nov 12 blah blah blah blah cat4
0
source

All Articles