Two shell variables using nawk

How do you use two shell variables in awk? I am using Solaris 10 and don't have GNU products installed.

File (transportation.txt) contents:
car make
boat model
airplane landing
snowmobile track
bicycle helmet
sled housing

Thanks to this forum this code works (prints everything from the contents of $vehicle to bicycle):
nawk -v v="$vehicle" 'match( $0, v ),/bicycle/' transportation.txt

But how do I replace bicycle with another variable? For arguments sake call it $vechicle2.

Also, will the above nawk code work with a string? For example if the contents of $vechicle equals boat model

vehicle1=car
vehicle2=bicycle
nawk -v v1="$vehicle1" -v v2="$vehicle2" 'match($0,v1),match($0,v2)' transportation.txt
1 Like

Will this code work for strings? Like if $vechicle1=car make and $vechicle2=bicycle helmet

Provided that you quote the variables as you use them, as chihung has done, then multi token strings (with spaces) should be fine.

1 Like

Alternatively, try:

awk '$0~v1,$0~v2' v1="$vehicle1" v2="$vehicle2" infile

On Solaris use /usr/xpg4/bin/awk (or nawk, but not awk)