Bash – how to send output of curl request to 2 separate commands

bashcommand-substitutiongawk

I'm piping the output of a curl request to gawk so that I can pull some data.
The gawk code is already working if I'm searching through the html file which curl request produces.
However, I was hoping to simply edit in place, as I just need to allocate 2 date variables. I've been looking at using tee and process substitution.
Is there any way to somehow fork the output of the curl request in order to assign 2 different variables, both using gawk and command substitution.

Here's a snippet of my code, I've hidden the full details of the curl request, but it is going to pull an html report which will contain: Oldest Sequence, date and Newest Sequence, date on separate lines. The gawk has already been tested on the data.

curl -k -q "https://user:password@ipaddress/report" 

I want the output of the above command to be piped or otherwise to the two commands below

date1="$(date -d $(gawk '/Newest Sequence/ {print $3,$4}') +%s)"
date2="$(date -d $(gawk '/Oldest Sequence/ {print $3,$4}') +%s)" 

Is this possible without creating a file? I will be running the request at frequent intervals and sending the result to a socket, I'd rather avoid unnecessary files if possible

Best Answer

The basic syntax you need is this:

read date1 date2 < <( curl ... | gawk '...' )

This way you need just one awk instance as illustrated here (without the seconds conversion which you'd have to add; see below):

read date1 date2 < <( curl ... |
    awk '
      /Newest Sequence/ { new=$3" "$4 }
      /Oldest Sequence/ { old=$3" "$4 }
      END { print new, old }
      '
    )

(If the order of the dates in the HTML file is fixed that could be simplified by immediately printing the date information.)

Note that gawk has also the necessary time functions so that your date based code will become obsolete. In the code I showed you'd need to add awk's mktime() calls, or alternatively (to avoid gawk's time functions) do the conversion on shell level with date modifying the variables, as in:

date1=$(date -d "${date1}" +%s)
date2=$(date -d "${date2}" +%s)
Related Question