Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add empty line between each time blocks

Tags:

bash

sed

awk

I have the input log a form:

system 2018-02-05 04:15:49 :: aaaaaaaaaaaaa  
system 2018-02-05 04:15:51 :: aaaaaaaaaaaaa  
system 2018-02-05 04:15:51 :: aaaaaaaaaaaaa  
system 2018-02-05 04:15:52 :: aaaaaaaaaaaaa  
system 2018-02-05 04:15:53 :: aaaaaaaaaaaaa  
system 2018-02-05 04:20:06 :: ccccccccccccc
system 2018-02-05 04:21:10 :: bbbbbbbbbbbbb
system 2018-02-05 04:21:10 :: ccccccccccccc
system 2018-02-05 04:21:10 :: ccccccccccccc
system 2018-02-05 04:21:10 :: ccccccccccccc
system 2018-02-05 04:23:49 :: bbbbbbbbbbbbb
system 2018-02-05 04:23:49 :: ccccccccccccc

and want to have separated each time block by empty line. Expected output for above input would be:

system 2018-02-05 04:15:49 :: aaaaaaaaaaaaa

system 2018-02-05 04:15:51 :: aaaaaaaaaaaaa  
system 2018-02-05 04:15:51 :: aaaaaaaaaaaaa  

system 2018-02-05 04:15:52 :: aaaaaaaaaaaaa  

system 2018-02-05 04:15:53 :: aaaaaaaaaaaaa  

system 2018-02-05 04:20:06 :: ccccccccccccc

system 2018-02-05 04:21:10 :: bbbbbbbbbbbbb
system 2018-02-05 04:21:10 :: ccccccccccccc
system 2018-02-05 04:21:10 :: ccccccccccccc
system 2018-02-05 04:21:10 :: ccccccccccccc

system 2018-02-05 04:23:49 :: bbbbbbbbbbbbb
system 2018-02-05 04:23:49 :: ccccccccccccc
like image 321
Chris Avatar asked Dec 10 '22 08:12

Chris


1 Answers

The idea is to form the key that each of the lines are unique with, in your case it is $2 and $3 (i.e. in Awk's context second and third space delimited columns).

We build a unique key($2 $3) by this combination and while parsing the lines if this combination varied from the subsequent line, we print a new line character (also represented by special variable ORS or just print "" in Awk). The below code reflects just that

$ awk '($2 $3)!=p && NR>1 {print ""} {print; p=($2 $3)}' file
system 2018-02-05 04:15:49 :: aaaaaaaaaaaaa  

system 2018-02-05 04:15:51 :: aaaaaaaaaaaaa  
system 2018-02-05 04:15:51 :: aaaaaaaaaaaaa  

...
like image 86
James Brown Avatar answered Dec 29 '22 12:12

James Brown