Search code examples
linuxawksedcut

How to print groups of lines from a log file, transposing them into single line


I am trying to print error logs in my server into a single line with just time and important detail for failure. 1st line contains the time stamp of log and last line above new line contains reason for the log. Wanted to align them into single line. Any help is appreciated. "Need the timestamp , severity, last line just before the blank line" in a single line.

Current log structure:

MAR31 10:30 critical
cable is faulty

MAR31 10:31 info
Card is recovered

MAR31 10:31 critical
Another card failed

MAR31 10:31 critical
missing inventory
cable missing

.....
.....
.....

.....
.....

......
......

required log structure is :

MAR31 10:30 critical cable is faulty

MAR31 10:31 info Card is recovered

MAR31 10:31 critical Another card failed

MAR31 10:31 critical cable missing

Solution

  • With awk:

    awk -v RS= -F '\n' '{ print $1, $NF }' filename
    

    An empty record separator separates records at empty lines, and with the field separator set to a newline, each field is a line. print $1, $NF then prints the first and last lines of the record.