Using awk for a table lookup

awk

Solution

Something like this would do the job:

#!/bin/sh
awk -vLOOKUPVAL=$1 '$1 == LOOKUPVAL { print $2 }' < inputFile

Essentially you set the lookup value passed into the shell script in $1 to an awk variable, then you can access that within awk itself. To clarify, the first $1 is the shell script argument passed in on the command line, the second $1 (and subsequent $2) are fields 1 and 2 of the input file.

Problem

I would like to use awk to lookup a value from a text file. The text file has a very simple format: text \t value text \t value text \t value ... I want to pass the actual text for which the value should be looked up via a shell variable, e.g., $1. Any ideas how I can do this with awk? your help is great appreciated. All the best, Alberto

Original source