0

I have the following lines result from zcat command

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]}>

I want to add the line 2 result from call to first line 1 like this

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]}>

Can I do that?

sseLtaH
  • 2,706
  • 1
  • 6
  • 19
  • 1
    Is your input actually three lines? You mention a second line, but you show three lines. Also, is the input exactly as shown, with the numbering, or is some part of that data just annotation? – Kusalananda May 12 '22 at 06:19

3 Answers3

0

Using 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]}>
sseLtaH
  • 2,706
  • 1
  • 6
  • 19
0

How about the simple awk command:

$ 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]}>

It takes care of empty lines by disregarding them.

It supposes that:

  • your two non-empty lines always appear in a fixed sequence order, i.e. first 1 - URL Template: ... then 2 - Response: ....
  • no other non-empty lines appear between the two lines you show. If otherwise, then additional logic is needed. Such a change would be straightforward.
  • each of the two strings "1 - " and "2 - Response: " appear only once per line and at their beginning.
Cbhihe
  • 2,549
  • 2
  • 21
  • 30
-1
 sed '/^$/d' filename| awk 'NR==2{gsub(/.*Response:/,"",$0)}1'|sed "1s/.*-//g"|perl -pne "s/\n/ /g"

output

 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]}>
Praveen Kumar BS
  • 5,139
  • 2
  • 9
  • 14