Computing.Net > Forums > Unix > Awk getting the quotes right with variable.

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Awk getting the quotes right with variable.

Reply to Message Icon

Name: TCStuff
Date: April 23, 2009 at 07:16:35 Pacific
OS: UNIX
Subcategory: General
Comment:

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

This 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



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: April 23, 2009 at 11:05:57 Pacific
Reply:

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.


0

Response Number 2
Name: James Boothe
Date: April 23, 2009 at 12:03:16 Pacific
Reply:

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"


0

Response Number 3
Name: TCStuff
Date: April 23, 2009 at 12:18:16 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More






Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: Awk getting the quotes right with variable.

AWK get filename www.computing.net/answers/unix/awk-get-filename/7024.html

calculating expressions with awk www.computing.net/answers/unix/calculating-expressions-with-awk/4834.html

how to get the last field www.computing.net/answers/unix/how-to-get-the-last-field/6529.html