How Do I Use cut When My Output Uses Spaces?

The cut command is very useful for picking things like finding the PID of a process to kill. However, the default delimiter is TAB, making the cut command rather difficult to use. The solution is to squeeze the redundant spaces into a single space using tr, then select the fields using cut. For instance this shell function creates the mycut command:

function mycut()
{
  tr -s ' ' | cut -d' ' $*
}

Using mycut is simple, let's use it with ps to get the PIDs of Terminal apps on Mac OS X:

$ ps ax|grep Terminal|grep -v grep
 1081   ??  R     11:39.70 /Applications/Utilities/Terminal.app/Contents/MacOS/Terminal
$ ps ax|grep Terminal|grep -v grep|mycut -f2
1081
How Do I Use cut When My Output Uses Spaces?
Share this