如何在Linux Shell脚本中合并文本文件中同一行中的两行 [英] How to merge two rows in a same row from a text file in linux shell script

查看:936
本文介绍了如何在Linux Shell脚本中合并文本文件中同一行中的两行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Nagios使用wget命令下载了html文件,然后使用以下代码将该htmlfile转换为Textfile:

From Nagios I downloaded its html file by using wget command and then I converted that htmlfile to Textfile by using following code:

html2text -width 180 file.html >a.txt

然后我剪切了前10行,因为我不想要该文本,并且得到了低于textfile输出的结果

then I cut the first 10 lines becoz I dont want that text and I got below textfile output

awk 'NR > 10 { print }'a.txt > b.txt

我必须将两行合并为单行,而不是仅对b.txt文件中的特定输出的所有行进行合并. 注意:该文本文件包含N行数

I have to merge two rows into single row not for all lines only for particular output from b.txt file. Note: the text file contains N number of lines

这是b.txt文件的输出:

Here the b.txt File Output:

                      DISK OK - free space:          CRITICAL
01-08-2018 07:05:05   Service Required     Critical  CPU:loadaverage 6.0%                    

01-08-2018 07:10:25   Service Alert        Critical  memoryUsage

                       DISK OK - free space: 
02-08-2018 01:05:2018  Service Alert       Warning   memoryUsage

                                                      CRITICAl:outstanding alert attention 
02-08-2018 02:05:2018  Service Alert        Critical  required 

预期输出:

01-08-2018 07:05:05   DISK OK - free space:Service Required  Critical    CRITICALservice requiredCPU:loadaverage 6.0%

01-08-2018 07:10:25   Service Alert                          Critical    memoryUsage

02-08-201801:05:2018  DISK OK - free space:Service Alert     Warning     memoryUsage

02-08-2018 02:05:2018 Service Alert                         Critical     CRITICAL:outstanding alert attention required

预先感谢

推荐答案

您可以使用awk将行合并在一起:

You can use awk to merge lines together:

awk '
  /^ +/{                     # For lines starting with spaces,
    gsub(/^ +/," ");         # Replace these multiple spaces with only one
    a=a $0;                  # Store the line into the variable a
    next                     # Continue with the next line
  }
  {                          # For lines not starting with spaces
    $3=$3 a;                 # Append the variable a to the third element
    a=""                     # Clear variable a
  }
  1                          # Print the current line
' b.txt

这篇关于如何在Linux Shell脚本中合并文本文件中同一行中的两行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆