by fiddlerwoaroof · 9y ago · 0 comments
> Pulling out a field from a line is what most people use awk for, but it's honestly the least interesting part of awk. In fact, if cut supported regular expressions for specifying the field and record separators people wouldn't be using awk for that purpose (because that's all that $n does).
There's nothing magical about awk's default FS. It's literally just /\s+/. If cut's -d was slightly more clever you wouldn't need to use awk.
Also, cut's output manipulation is surprising. '-f 2,1' is actually the same as '-f 1,2' - you can't change the order of printing.
I know there are other programs that can do the job, but it's a little frustrating when you can 'almost' get there with a chain of piped commands and a simple tool like cut, but have to fall back on a 'real' programming language to do just that extra bit of manipulation (awk, perl, whatever, and yes, I know the shell is a programming language but you get my point!)
some-cmd | sed -E 's/^\s+//;s/\s+/ /g' | cut -f$n
Rather than some-cmd | awk '{ print $n }'
;) Also I agree it's silly you can't change the field order. some-cmd | sed -E '{ s/^\s+//g ; s/\s+/ /g }' | cut -f$n