微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在Linux Shell脚本中合并文本文件中同一行中的两行

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

html2text -width 180 file.html >a.txt

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

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

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

这里是b.txt文件输出

                      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将行合并在一起:

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐