views:

57

answers:

2

I want to invoke curl (which invokes a REST service) for every matching row processed via awk.

Input file (delimited by colon). e.g.

$ cat /tmp/input

tiger:EN
haiti:FR
federer:CH

I got as far as this:

awk -F':' '{print $1 "=" $2}' /tmp/input

This just confirms that I can extract the columns out correctly.

Now I need to invoke the REST service like this:

curl -XPOST -H "Content-Type: application/json"
   -d "{ lang: EN, name: tiger }" http://server/rs/user

Is it possible to write a one-liner in awk to do this, or should I write a bash script? Thanks

+1  A: 

A bash script would be simpler. Set $IFS to : and then use read.

Ignacio Vazquez-Abrams
+3  A: 
while IFS=":" read -r lang name
do
    curl -XPOST -H "Content-Type: application/json" -d "{ lang: $lang, name: $name }" http://server/rs/user
done <"file"

Or you can use awk as well

awk -F":" '
{
    cmd="curl -XPOST -H \042Content-Type: application/json\042 -d \042{ lang: "$2", name: "$1" }\042 http://server/rs/user"
    system(cmd)
}
' file
ghostdog74