Skip to content
Advertisement

How can I extract parameters of curl request and append them to beginning of txt file using bash

I have a bash file which is executed every 5 seconds, it goes like this:

#! /bin/sh
curl -I http://192.168.100.16:4000/status >> /home/debian2/debian0_ping.txt

It pings to a server running in a neighbor Virtual Machine and records the curl response into the specified file “debian0_ping.txt”

but the recorded response looks like this:

HTTP/1.1 200 ok <--- First Ping
X-Powered-By: Express
Content-Tye: text/plain; charset=utf-8
Content-Length: 2
Etag: "/2-n009QiTIwXgNt...."
Date: Tue, 17 Aug 2021 18:56:25 GMT
Connection: keep-alive

HTTP/1.1 200 ok <--- Second Ping
X-Powered-By: Express
Content-Tye: text/plain; charset=utf-8
Content-Length: 2
Etag: "/2-n009QiTIwXgNt...."
Date: Tue, 17 Aug 2021 18:56:28 GMT
Connection: keep-alive

Which is a problem because I need the pings responses to be recorded this way (the most recent on top):

HTTP/1.1 200 ok <--- Second Ping
X-Powered-By: Express
Content-Tye: text/plain; charset=utf-8
Content-Length: 2
Etag: "/2-n009QiTIwXgNt...."
Date: Tue, 17 Aug 2021 18:56:28 GMT
Connection: keep-alive

HTTP/1.1 200 ok <--- First Ping
X-Powered-By: Express
Content-Tye: text/plain; charset=utf-8
Content-Length: 2
Etag: "/2-n009QiTIwXgNt...."
Date: Tue, 17 Aug 2021 18:56:25 GMT
Connection: keep-alive

Finally I need to record only the first line (HTTP/1.1 200 ok) and the date line (Date: …) I’ve tried using SED commands, but it doesn’t seems to take variables as parameters to append onto the file.

Please, how can I achieve this, I know it’s simple but I’m too new at automating things.

Advertisement

Answer

There is no straight-forward way to do reverse the output in bash.

To limit what lines you’re getting you can use the pattern grep 'pattern1|pattern2'.

You can do something like this to get the output in the desired format. NOTE: the output file must exist before running this.

curl -I https://www.google.com | grep 'HTTP|Date' | cat - /tmp/blah.txt > temp && mv temp /tmp/blah.txt

Output

HTTP/2 200
date: Tue, 17 Aug 2021 20:38:10 GMT
HTTP/2 200
date: Tue, 17 Aug 2021 20:38:07 GMT
HTTP/2 200
date: Tue, 17 Aug 2021 20:38:05 GMT
HTTP/2 200
date: Tue, 17 Aug 2021 20:38:03 GMT

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement