Gawk Question

I am trying to use gawk to search a file and put the second value of the string into a string.

          gawk -F: '$1~/CXFR/ {print $2}' go.dat

go.dat

HOME      :/
CTMP       :/tmp
CUTL       :/u/rdiiulio/bin
CWRK      :/u/work
CXFR       :/u/xfer

====================

So when I type the command manually on the screen, I get the string
'/u/xfer'. However, when I use a shell script to do the same thing, it does not work. Here is the command in the shell script:

          gawk -F: '$1~/$chkstr/ {print $2}' go.dat

I set the variable 'chkstr' to '$1' and it does not work. Anybody have a answer to this? Any help will be very much appreciated.

You can't mix shell variables and awk variables inside an awk script. And, a shell variable won't be expanded inside single quotes.

Making some wild guesses, try:

chkstr="$1"
gawk -v chkstr="$chkstr" -F: '$1~/chkstr/ {print $2}' go.dat

Tried your suggestion and it did not work.

That sure helps us track down the problem...

Show us your exact script.

Show us the exact command you used to invoke it.

On the other hand, it looks like I left in too much of your original code. Try:

chkstr="$1"
gawk -v chkstr="$chkstr" -F: '$1~chkstr {print $2}' go.dat

(i.e., remove the / characters surrounding chkstr in the awk script).

That worked, thank you Don.