col() { awk '{print $'$(echo $* | sed -e 's/ /,$/g')'}'; }
Something I do a lot is extract columns from some input where cut
is not suitable because the columns are separated by not a single character but multiple spaces or tabs. So I often do things like:
... | awk '{print $7, $8}'
... which is a lot of typing, additionally slowed down when typing symbols like '{}$
... Using the simple one-line function above makes it easier and faster:
... | col 7 8
How it works:
col
awk
, and it expects standard input (coming from a pipe or input redirection)sed
to use them with awk
: replace all spaces with ,$
so that for example 1 2 3
becomes 1,$2,$3
, which is inserted into the awk
command to become the well formatted shell command: awk '{print $1,$2,$3}'
col() { awk '{print $('$(echo $* | sed -e s/-/NF-/g -e 's/ /),$(/g')')}'; }