AWK vs CUT

Well, not really.  But I assume I’ll get lots of attention with that name alone.  When I thought about writing this it was for one very specific thing only.

Often times in a script I’ll want to strip output based on a delimiter or columns.  I found myself constantly fighting with IFS and trying to make sense of delimiters that don’t follow a common pattern.  My two ways of extracting columns from a file/variable are as follows:

 

echo $variable | cut -f 2 -d “:” –> (-f to select column, -d to select delimiter)

echo $variable | awk ‘{ print $2 }’

Believe it or not, for someone that started off with no formal shell training or anything, this took me awhile to figure out.  Cut has its ups and downs but I will say this, awk is without a doubt worth learning.  In its entirety.

Anyways, this was just a quick update on how to extract columns from data using cut and awk

Leave a reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>