cut
is an incredibly use command but it can be frustrating to use on columar data with a variable number of spaces between columns. awk
is often a better alternative in that case but, IMHO, it often feels more nature to use cut
in a shell pipeline rather than a quoted onliner awk
expression.
I recently stumbled across this trick (which I suspect I’ve discovered and forgotten more than once) of using tr
to compress multi-space field seperators into a since character.
Consider this simple example of finding the ammount of free memory on the system:
$ free total used free shared buffers cached Mem: 7731688 5692336 2039352 396756 740020 2239136 -/+ buffers/cache: 2713180 5018508 Swap: 7733244 0 7733244 $ free | grep Mem: | cut -d ' ' -f4 $ free | grep Mem: | tr -s ' ' | cut -d ' ' -f4 2037816