如何从日志文件中打印行组,将它们转换为单行

时间:2022-05-23 21:46:07

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

1 个解决方案

#1


2  

With awk:

用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.

空记录分隔符以空行分隔记录,并且字段分隔符设置为换行符,每个字段都是一行。 print $ 1,$ NF然后打印记录的第一行和最后一行。

#1


2  

With awk:

用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.

空记录分隔符以空行分隔记录,并且字段分隔符设置为换行符,每个字段都是一行。 print $ 1,$ NF然后打印记录的第一行和最后一行。