linux - 如何从linux shell脚本中的文本文件合并同一行中的两行
问题描述
从 Nagios 我使用 wget 命令下载了它的 html 文件,然后我使用以下代码将该 htmlfile 转换为 Textfile:
html2text -width 180 file.html >a.txt
然后我剪掉了前 10 行,因为我不想要那个文本,我得到了低于文本文件输出
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
推荐阅读
- botium-box - Botium 语音处理中的文本到语音 API,在 Swagger UI 中进行测试时给了我一个 ECONNREFUSEDError 作为响应
- python - 将两个 numpy 矩阵合并为一个
- html - css3 flex:为什么 img 占用的宽度大于包含的宽度?
- django - 在接受多个文件上传的 FileField 上迭代 Django 表单
- go - 处理服务配置的最佳方式是什么?
- android - 在 Android 和 iOS 中调用 Twilio(跨平台问题)
- symfony - 是否有另一种方法来排除 Homestead 中的文件夹而不是使用 rsync?
- ios - 升级版本数据库后Service Extension中的核心数据
- javascript - 为什么这个 JavaScript 算法没有定义?
- bash - 每个序列(seq 1-current)在两个不同目录中的文件数量的数值差异是多少?