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 -F 1

Status
Not open for further replies.

vodkadrinker

Technical User
May 16, 2002
163
GB
If run the following command from the command line it works ok.

awk -F ";" '$2 == "123"' /tmp/testfile

However if I set a variable first it errors awk: 0602-562 Field $() is not correct.

myvalue="123"
awk -F ";" '$2 == $myvalue' /tmp/testfile

cat testfile
aa;1;mmm
bb;123;zzz
aa;123;ppp
cc;222;yyy

Any help would be greatfull.

Thanks,

VD
 
Hi

The shell does not perform variable expansion in strings enclosed in single quotes.
Code:
awk -F ";" [red]"\[/red]$2 == $myvalue[red]"[/red] /tmp/testfile

[gray]# or[/gray]

awk -F ";" '$2 == [red]'"[/red]$myvalue[red]"[/red] /tmp/testfile

[gray]# or[/gray]

awk -F ";" [red]-vmy="$myvalue"[/red] '$2 == [red]my[/red]' /tmp/testfile


Feherke.
 
Many thanks this one works great but the other two didn't seem to work.

awk -F ";" -vmy="$myvalue" '$2 == my' /tmp/testfile
 
Hi

All three works the same way for the test data you posted. Based on your reply, probably you are trying to searched for non-numeric value. In that case the other two codes need minor modifications :
Code:
awk -F ";" "\$2 == [red]\"[/red]$myvalue[red]\"[/red]" /tmp/testfile

[gray]# or[/gray]

awk -F ";" '$2 == [red]"[/red]'"$myvalue"[red]'"'[/red] /tmp/testfile

Feherke.
 
Thanks Feherke your right I am using non-numeric's, sorry about that I was trying to give my example in the simplist form but missed the obvious.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top