awk - How to search a pattern text and transpose in a single line till the next time same text is found? -


i trying parse logfile timestamp header in square bracket. if square bracket found lines below transposed until next time same square bracket found? have data in format:

[2017-03-31 01:15:08.000] lteevent event { recordlength 4, recordtype 0, fileheader { file-format-version t, pm-recording-version c, pm-recording-revision r, year 2, month 3, day 3, hour 1, minute 1, second 8, ne-user-label ld, ne-logical-name ld } }  [2017-03-31 01:15:08.000] lteevent event { recordlength 1, recordtype 3, scannerconnection { hour 1, minute 1, second 8, millisecond 0, scanner-id '0'h, status 2, padding '0'h } } , on......the number of lines after[date time] lte event not fixed , keeps on changing 

i looking output in excel like:

[2017-03-31 01:15:08.000] lteevent  lteevent {  recordlength 417     [2017-03-31 01:15:08.000] lteevent  lteevent {       

some more

input

$ cat file [2017-03-31 01:15:08.000] lteevent lteevent { recordlength 417, [2017-03-31 01:15:08.000] lteevent lteevent { 

output

$ awk -v rs=, '{ $1=$1 }1' file [2017-03-31 01:15:08.000] lteevent lteevent { recordlength 417 [2017-03-31 01:15:08.000] lteevent lteevent { 

explanation

  • -v rs=, record separator set comma

  • $1=$1 when $1=$1 (or other assignment field) causes record recompilation $0 rebuilt every fs replaced ofs, set output field separator(ofs) default ( single space), newline char removed.

  • 1 default action { print $0 }

or

$ awk '/^\[/{if(s)print s;s=""}{sub(/,$/,"");s =(s ? s ofs:"") $0}end{if(s)print s}' file [2017-03-31 01:15:08.000] lteevent lteevent { recordlength 417 [2017-03-31 01:15:08.000] lteevent lteevent { 

explanation

awk '/^\[/{                               # search line starts [          if(s)print s;s=""                # if variable s has print , reset      }      {        sub(/,$/,"");                      # remove comma before line terminator record        s =(s ? s ofs:"") $0               # concatenate variable s current record      }   end{                                    # end block        if(s)print s                       # if s has print       }' file 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -