Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I think this has to do with the quoting, I just feel I've been looking at it too long. Thanks ~T
prompt> cat my.awk
BEGIN{"date +%d%b%Y.%H%M%S" | getline sDate}
{
if (substr($0,151,1) ~ /6/ )
print >> sDate".NEW_ORDER.dat"
# print >> sDate # note this works to output the contents to sDate, but want filename with the date like the print statement above
else
print >> sDate."OLD_ORDER.dat"
}prompt> nawk -f my.awk Testfile
Desired output is two files:
21Apr2009.184534.NEW_ORDER.dat
21Apr2009.184534.OLD_ORDER.datThis is what I am getting:
prompt> nawk -f my.awk Testfile >
nawk: syntax error at source line 7
context is
print >> sDate >>> ".NEW_ORDER.dat" <<<
nawk: illegal statement at source line 7

Sorry, but awk is too brain dead to do what you want. The print command can only be redirected to a file name surrounded by double quotes - essentially a constant.
You can not do this:
print >> sDate".NEW_ORDER.dat"
but you can do this:
print >> "NEW_ORDER.dat"
You can change your awk logic to maybe use the system command to do a rename or mv command. Check out this awk stub:
echo ""|nawk '
BEGIN{"date +%d%b%Y.%H%M%S" | getline sDate}
{print "mystuff" >> "NEW_ORDER.dat"
str="mv NEW_ORDER.dat "sDate ".NEW_ORDER.dat"
system(str)
} '
Or you might consider performing the mv outside the awk script.

Actually, the targeted filename does not have to be a simple quoted constant, but in fact can be a variable or even the concatenation of a variable and constant like TCStuff is attempting.
His first print statement is correct, but his second print statement has the dot incorrectly placed:
wrong: print >> sDate."OLD_ORDER.dat"
correct: print >> sDate".OLD_ORDER.dat"

I removed the else from the awk this is what I had - it was causing the error:
prompt> cat my.awk2
BEGIN{"date +%d%b%Y.%H%M%S" | getline sDate}
{
if (substr($0,151,1) ~ /6/ )
print >> sDate".NEW_ORDER.dat"
}
prompt> nawk -f my.awk2 Testfile
/usr/bin/nawk: syntax error at source line 4
context is
print >> >>> sDate".NEW_ORDER.dat" <<<
/usr/bin/nawk: illegal statement at source line 4

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |