I have a text file like so
foo bar baz
1 a alpha
2 b beta
3 c gamma
I can use awk to print certain columns, like 1 and 3, with {print $1, $3}
, but I want to specify the columns to print by specifying the header of the column instead, something like {print $foo, $baz}
. This is useful so I don't have to open the file and count the columns manually to see which column is which, and I don't have to update the script if the column number or order changes. Can I do this with awk (or another shell tool)?