Computing.Net > Forums > Unix > Square brackets as string in awk

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

Square brackets as string in awk

Reply to Message Icon

Original Message
Name: Shannon
Date: June 24, 2003 at 08:16:36 Pacific
Subject: Square brackets as string in awk
OS: COMPAQ Evo
CPU/Ram: 128MB
Comment:

I am trying to write a script which will create a parameter file containing [-a d][-i userid] with userid coming from an sql query. I keep getting an error due to the square brackets w/in the variable: awk: Field $([-a d]) is not correct. The line causing the error is as follows: cat n585inactiveuser1.out | awk -v var1="$ws_nodays1" -v var_expr="\[-a d\]" -v
var_expr1="\[ -i" -v var_expr2="\]" -F"|" ' I have tried several different ways of passing the square brackets through a variable but keep getting the same error. Any suggestions?


Report Offensive Message For Removal


Response Number 1
Name: James Boothe
Date: June 24, 2003 at 12:24:52 Pacific
Reply: (edit)

Those -v variables, as you have coded, come into my awk just fine. Your problem might be in the body of your script, which I cannot see.

However, there is no need to feed literals in as variables. Here is a print statement to print a combination of string literals and $2:

echo "word1 word2 word3" |
awk '{print "[-a d] [-i " $2 "]"}'

and here is printf statement that defines a format mask and one replacement token (%s for a string), to be filled by $2:

echo "word1 word2 word3" |
awk '{printf "[-a d] [-i %s]\n",$2}'

Both of the above produce:

[-a d] [-i word2]


Report Offensive Follow Up For Removal







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home








Do you have your own blog?

Yes
No
I did before
I will soon


View Results

Poll Finishes In 5 Days.
Discuss in The Lounge
Poll History




Data Recovery Software