Hacker News new | past | comments | ask | show | jobs | submit login

   echo '1 2 3 4' | cut -d' ' -f1
By default cut uses TAB as delimiter



Not to be picky, but still not good enough:

   echo '1    2     3 4' | cut -d' ' -f2
'cut' fails when fields are separated by multiple spaces.


I usually pipe through a whitespace-to-single-tab command. So

  echo '1   2  3 4' | sp2tab | cut -f2
would be equivalent to

  echo '1   2  3 4' | awk '{print $2}'
but quicker to write.


Ok.

   > echo '1    2         3 4' | sed 's/  */\t/g' | cut -f2
    2


echo "1 2 3 4" | tr -s " " | cut -f1 -d " "

tr is nice!


Doesn't work, gets caught on leading spaces:

    echo ' 1 2 3 4' | tr -s " " | cut -f1 -d " "
    >
    echo ' 1 2 3 4' | awk '{print $1}'
    >1




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: