Question

I have used Sed to grab two interesting values. Now I want to send those two values as parameters to Curl. I have been successful piping Sed output to Curl with only 1 argument using xargs, however I am unable to use two arguments for one command.

echo "value1" "value2" | curl --data 'valA=$1&valB=$2' http://example.com

I am stuck in both theory and practice. I wasn't planning to use bash scripting.

[ I am running tshark, piping output to sed, and hoping to pipe that output to curl so as to record data in a remote DB. ]

Was it helpful?

Solution

As long as you're not expecting to use valA and valB in future commands, you can use read to store the the whitespace-delimited output from your exiting commands:

$ echo foo bar | { read var1 var2 ; echo $var1 $var2 ; }
foo bar

Which means you can do:

echo "value1" "value2" | { read a b ; curl --data 'valA=$a&valB=$b' http://example.com ; }

OTHER TIPS

Assuming you can get sed to output something like .csv (foo\tbar):

tshark | sed ... | parallel --colsep '\t' -q curl --data 'valA={1}&valB={2}' http://example.com

You can find more about GNU Parallel at: http://www.gnu.org/s/parallel/

Watch the intro video on http://www.youtube.com/playlist?list=PL284C9FF2488BC6D1

Walk through the tutorial (man parallel_tutorial). Your command line will love you for it.

Backticks sound like the best bet, if you can't run the sed twice (eg you're doing this on a live tshark stream):

curl --data $(printf "valA=%s&valB=%s" `tshark --whatever | sed -e whatever`)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top