Add blank line between time blocks

I have 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 you want to split each time block into an empty line. Expected result to enter above:

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
+4
source share
5 answers

The idea is to form a key, in which each row is unique in your case it $2and $3(i.e., in the context Awk context - delimited columns of the second and third spaces).

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

$ 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  

...
+6

awk :

awk 'NR>1 && !a[$2$3]++{ print "" }1' file
  • !a[$2$3]++ - datetime $2 $3

:

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
+4

awk

filter .

#!/bin/bash

Year='$2'
Month='$2$3'
Day='$2$3$4'
Hour='$2$3$4$5'
Minute='$2$3$4$5$6'
Second='$2$3$4$5$6$7'

filter=$Second

awk -F'[ :-]' $filter'!=p{print ""}{p='$filter'}{print}' sys.log

https://github.com/tigertv/stackoverflow-answers

+2

(GNU sed):

sed 'N;/^\(.*::\).*\n\1/!P;//!s/^[^\n]*//;P;D' file

. , . , . , , .

:

sed 'N;P;/^\(.*::\).*\n\1/D;s/^[^\n]*//;P;D' file

. , , , .

+1

bash for grep. ( , , ), :

IFS=$'\n'
for i in $(cut -d ' ' -f1-3 input_file.txt | sort -u)
    do 
        grep $i input_file.txt
        echo
    done

The change IFSensures that the loop forreads the variables as strings, not just records (see more details here ). Then the variable $istores the unique values ​​of the first three fields, and then grepthose that are in the source file, entering an empty line with echoafter each iteration. Now I understand that this reads the input_file twice, but anyway I hope this helps.

0
source

Source: https://habr.com/ru/post/1693177/


All Articles