cut with multiple spaces

Today’s little command line lesson.

Unix cut command takes a parameter which tells it the delimiter character to use. Often this is a space. However it only ever matches a single character, so when you have a line with extra spacing to make some columns line up, this doesn’t work right.

Instead of just using cut, you can pipe it through tr first like this, which collapses multiple spaces into one.

grep 'foo' mylogfile | tr -s ' ' | cut -f3 -d' '


One Comment

  1. steveo
    Posted February 4, 2009 at 11:59 pm | Permalink

    or replace
    tr -s ‘ ‘
    sed -r ‘s/ +/ /g’

    Sure the tr command is more compact, but I know the syntax for sed from using it for so many other things.

Post a Comment

Your email is never published nor shared. Required fields are marked *