Hi, I recently started working using awk. I am facing a problem to traverse a file row by row.
I have a file test.txt which has 2 fields filename and action.
test.txt
--------
/tmp/a.txt|Action1
/tmp/b.txt|Action2
/tmp/c.txt|Action3
I need to check for the existence of file ($1) in the above file, if true copy the action ($2) to a temporary file /usr/temp.txt and i am using this file as input to another script. Below is the awk i am using,
awk -F"|" '{
if (!system("test -f " $1))
{
printf "%s exists",$1
printf "%s", $2 > /usr/temp.txt
system("") -- Executing a script which takes temp.txt as input
system("rm -f " $1)
}
else
{
print $1 " Not exists"
}
}' /tmp/test.txt
The problem i am facing here is, if a.txt and c.txt are there in test.txt file, it is copying the Action1 and Action3 into the file temp.txt. I want only one Action at a time.
This is how i want. Check for first file existence if true copy the action to a file and go to next line. if not true go to next line. and so on..
Please help.