BEGIN {
FS=",";
}
{
system("curl -v -H \"Authorization: SSWS test" -H \"Accept: application/json\" -H \"Content-Type: application/json\" -X POST \"https://tes.test.com/api/v1/users?activate=false\" -d \'{ \"profile\": { \"firstName\": \"" $1 " \", \"lastName\": \"" $2" \", \"email\": \"" $3 "\", \"login\": \"" $4 "\" } }\'"| getline response);
#Printing the create user response
system("echo " response);
#get the UID from the response for adding to the group
system("echo " response "| cut -d' ' -f1 | cut -d':' -f2" | getline id);
#Add the created user to group from csv file
system("curl -H \"Authorization: SSWS test" -H \"Accept: application/json\" -H \"Content-Type: application/json\" -X PUT \"https://test.test.com/api/v1/groups/00g2msc01fEYNMEAXNMZ/users/" id "\"");
}
when i execute the command in my mac os
awk -f Finaluser.awk test1.csv
it creates 8 users fine in the 9th user i get a below error.
awk: echo {"id":"00u2mut1cf","status":"STAGED","created":"2014-08-19T06:03:52.000Z","activated":null,"statusChanged":null,"lastLogin":null,"lastUpdated":"2014-08-19T06:03:52.000Z","passwordChanged":null,"profile":{"firstName":"Adam ","lastName":"test ","email":"test.adam@2l.test","login":"test.adam@2l.test","mobilePhone":null},"credentials":{"provider":{"type":"TEST","name":"TEST"}},"_links":{"activate":{"href":"https://test.test.com/api/v1/users/00u2mut1cf/lifecycle/activate","method":"POST"},"deactivate":{"href":"https://test.test.com/api/v1/users/00u2mut1cf/lifecycle/deactivate","method":"POST"}}}| cut -d' ' -f1 | cut -d':' -f2 makes too many open files
input record number 9, file test1.csv
Using a system() function will only render an exit code within awk, not a response, the getline statements will not work like that and the quoting is not properly done. I would suggest using a shell script instead which really is better suited for this kind of application..
awk '
function exe_cmd(cmde,result){
while ((cmde | getline line) > 0)
{
result = result (result=="" ? "" : "\n")line
}
close(cmde)
return result
}
BEGIN{
command = "whoami"
me = exe_cmd(command)
print me
}'
or else try to write shell script as Scrutinizer suggested
IFS=,; while read firstname lastname email login etc; do
# your curl commands goes here...
echo $firstname $lastname $email $login
done < "your_csv_file"
Thank you so much . I am trying to do this in Shell. When i use the curl command and keeping firstname:"$f1" i am not getting the variable assigned properly. But when i do echo $f1 it prints be proper value. Any clue why curl is not taking the below below is my script.
!/bin/bash
IFS=","
while read f1 f2 f3 f4
do
echo "line is" : "$f4"
#Create user with credentials
(curl -v -H "Authorization: SSWS 00PM9livMo9jIpji" -H "Accept: application/json" -H "Content-Type: application/json" -X POST "https://test.test.com/api/v1/users?activate=false" -d '{ "profile": { "firstName": "$f1" , "lastName": "$f2" , "email": "$f3", "login": "$f4" } }')
echo $f1 $f2 $f3 $f4
done< /Users/kn/Downloads/test1.csv
~