Ads by Google

Showing posts with label awk. Show all posts
Showing posts with label awk. Show all posts

Saturday, October 3, 2009

Passing AWK variables to shell commands for evaluation

For some reason, I never seem to get how to use the shell from within GNU AWK to manipulate the AWK variable and return it. Spending a lot of time with the system and getline command, I still couldn't figure out how to pass a variable to a shell command, evaluate it and then return the value.

After much trial and error and googling, this seems to work.
a=20090601
cat ../data/C_100119.dat | \
awk -v compdate="$a" '{
while ("date --date="$4 " +%Y%m%d"|getline aa)
{
if(aa >= compdate)
print $0
else
exit
}
}'


where I'm extracting records whose 4th column date value is greater than a.

Hope this is useful to others.

Update: Scratch the above code. I got some feedback about the correct ways to use getline.
a=20090601
awk -v compdate="$a" '
{
if (("date --date="$4 " +%Y%m%d"|getline aa) >0){
if(aa >= compdate) {
print
next
}
}
exit
}' ../data/C_100119.dat



is a better way to do it.