Skip to content

Instantly share code, notes, and snippets.

@arodland
Created September 13, 2009 03:32
Show Gist options
  • Save arodland/186068 to your computer and use it in GitHub Desktop.
Save arodland/186068 to your computer and use it in GitHub Desktop.
get_field_index () {
FILENAME=$1
FIELD=$2
IFS=$'\t' read -a KEYS < $FILENAME
for i in `seq 0 $(( ${#KEYS[*]} - 1))` ; do
if [ "${KEYS[$i]}" = "$FIELD" ] ; then
echo $(( $i + 1 ))
return 0
fi
done
return 1 # not found
}
if LASAGNA=`get_field_index example.txt "Lasagna"` ; then
echo "Lasagna is \$$LASAGNA"
else
echo "Error!" 1>&2
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment