Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xargs: how to have literal double-quotes in replacement?

I have a source file with JSON-Objects one per line like this:

source:

{"_id":"1","name":"one"}
{"_id":"2","name":"two"}
{"_id":"3","name":"three"}

I want to send each line to a

curl -X POST -H "application/json" myURL -d '<REPLACEMENT>'

The double quotes do not make it to curl when I am trying

<source xargs -I % curl -X POST -H "application/json" myURL -d '%'

I tried escaping the quotes in the curl command and later I replaced all double-quotes in the source file with \". I found no version to work.

Another approach to use seq with sed to write each line into a temp file and curl -d @temp did not work out for me.

Is there an elegant solution or do I have to write a script with a loop?

like image 834
tschloss Avatar asked Jul 16 '14 15:07

tschloss


1 Answers

That's an interesting problem. There must be a better solution (perhaps konsolebox is onto something), but substituting all " with \" would work:

$ echo '"hello"'
"hello"
$ echo '"hello"' | xargs echo
hello
$ echo '"hello"' | sed 's/"/\\"/g' | xargs echo
"hello"
like image 62
ooga Avatar answered Oct 28 '22 13:10

ooga