如何在linux shell脚本中合并文本文件中同一行中的两行
我使用wget命令从Nagios下载了它的html文件,然后使用以下代码将该html文件转换为文本文件:如何在linux shell脚本中合并文本文件中同一行中的两行,linux,bash,shell,Linux,Bash,Shell,我使用wget命令从Nagios下载了它的html文件,然后使用以下代码将该html文件转换为文本文件: html2text -width 180 file.html >a.txt 然后我剪切了前10行,因为我不想要那个文本,我得到了下面的文本文件输出 awk 'NR > 10 { print }'a.txt > b.txt 我必须将两行合并成一行,而不是所有的行,只用于b.txt文件的特定输出。 注意:文本文件包含N行 这里是b.txt文件输出:
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
请避免“给我密码”的问题。而是显示您正在处理的脚本,并说明问题所在。还可以,谢谢。我得到了上述案例的解决方案我得到了正确的输出,但我得到了上述案例的错误我在上述答案中发布的任何解决方案请检查。@rajaviknesh没有任何错误描述和输入数据,很难判断出哪里出了错…我使用了awk命令(awk'/^+/{gsub(/^+/,“”);a=a$0;b=b$0;next}{$2=$2a;a=“}{$5=$5b;b=”“}1'b.txt),但我没有得到预期的输出。实际上我更改了问题场景,请再次检查,您将知道我想要下面的输出scenario@rajaviknesh请提出另一个问题,而不是改变问题的范围。