zcat
我的命令结果如下
1 - URL Template: https://www.test.com
2 - Response: <200 OK,{Server=[nginx], Date=[Wed, 11 May 2022 01:05:06 GMT], Content-Type=[text/html; charset=UTF-8], Transfer-Encoding=[chunked], Connection=[keep-alive], Vary=[Accept-Encoding]}>
我想将调用的第 2 行结果添加到第 1 行,如下所示
URL Template: https://www.test.com <200 OK,{Server=[nginx], Date=[Wed, 11 May 2022 01:05:06 GMT], Content-Type=[text/html; charset=UTF-8], Transfer-Encoding=[chunked], Connection=[keep-alive], Vary=[Accept-Encoding]}>
我可以这样做吗?
答案1
简单的命令怎么样awk
:
$ awk -F "1 - |2 - Response:" 'NF>1 {printf "%s",$2} END{print ""}' input_file
URL Template: https://www.test.com <200 OK,{Server=[nginx], Date=[Wed, 11 May 2022 01:05:06 GMT], Content-Type=[text/html; charset=UTF-8], Transfer-Encoding=[chunked], Connection=[keep-alive], Vary=[Accept-Encoding]}>
它通过忽略空行来处理它们。
它假设:
- 你的两个非空行总是以固定的顺序出现,即先
1 - URL Template: ...
然后2 - Response: ...
。 - 您显示的两行之间没有出现其他非空行。如果不是,则需要额外的逻辑。这样的改变是很简单的。
- 两个字符串“1 - ”和“2 - Response: ”每行仅在其开头出现一次。
答案2
使用sed
$ sed '/^1/{:a;N;s/^[^A-Z]*\(.*\)\n[^<]*\(.*\)/\1 \2/;ba}' input_file
URL Template: https://www.test.com <200 OK,{Server=[nginx], Date=[Wed, 11 May 2022 01:05:06 GMT], Content-Type=[text/html; charset=UTF-8], Transfer-Encoding=[chunked], Connection=[keep-alive], Vary=[Accept-Encoding]}>
答案3
sed '/^$/d' filename| awk 'NR==2{gsub(/.*Response:/,"",$0)}1'|sed "1s/.*-//g"|perl -pne "s/\n/ /g"
输出
URL Template: https://www.test.com <200 OK,{Server=[nginx], Date=[Wed, 11 May 2022 01:05:06 GMT], Content-Type=[text/html; charset=UTF-8], Transfer-Encoding=[chunked], Connection=[keep-alive], Vary=[Accept-Encoding]}>