Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

awk user defined variable help

Status
Not open for further replies.

pb0y

Technical User
Feb 27, 2002
16
US
X=some number

cat tmpfile | awk '$13~/"$X"/{print $0}'

what i want to do is to take a user defined variable and match it to field $13 in a text file. If the match is true then print the entire line. However, I don't seem to have the syntax correct for awk to understand $X is a variable. I have tried it also as:

cat tmpfile | awk '$13~/"'$X'"/{print $0}'
cat tmpfile | awk '$13~/\$X\/{print $0}'
cat tmpfile | awk '$13~/"${X}"/{print $0}'
cat tmpfile | awk -v X=$(X) '$13~/"$X"/{print $0}'

 
Thanks for your suggestion, however it seems there is a typo? I get

awk: syntax error near line 1
awk: bailing out near line 1

 
If on SUN,

cat tmpfile | nawk -v awkX=${X} '$13 ~ awkX {print $0}'
 

The variable declaration is working. The argument does not.

X=1234
cat tmpfile | awk -v awkX=${X} '$13 ~ awkX {print $0}'
+ cat tmpfile
+ awk -v awkX=1234 $13 ~ awkX {print $0}
awk: syntax error near line 1
awk: bailing out near line 1
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top