awk throws makes too many open files

Hi,

I have a below awk script.

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

Please throw some light on this.

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..

1 Like

@Scrutinizer
The above script is working fine. It does the job of executing my input file, I only have the error "makes too many open files"

---------- Post updated 08-20-14 at 06:48 AM ---------- Previous update was 08-19-14 at 12:50 PM ----------

Hi,

Appreciate some response. Come on help me Guru's..

Try this function

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"

@Akshay,

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
~                                                

Problem is with single quote replace it with double quotes..

-d  "{  'profile': { 'firstName': '$f1' ,  'lastName': '$f2' ,  'email': '$f3', 'login': '$f4' } }"

If i do double quotes. I get a error message that the server is expecting a single quote...:slight_smile:

Server is looking for the right format.

Try something like this

string="'""{  \"profile\": { \"firstName\": \"$f1\" ,  \"lastName\": \"$f2\" ,  \"email\": \"$f3\", \"login\": \"$f4\" } }""'"
curl -d $string