Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Don't forget about `cut -f1` though!

Of course anything more complex than that, but not requiring the complexity of Perl/Ruby is generally best still done with Awk. In my world, that is a lot.



cut -f1 and awk '{print $1}' are not equivalent, though: awk does not count empty fields. To be precise:

    pi:~$ echo 'foo  bar' | cut -d ' ' -f 2

    pi:~$ echo 'foo  bar' | awk '{print $2}'
    bar


Just for the interested reader: when the first command line is changed into

    pi:~$ echo 'foo  bar' | tr -s ' ' | cut -d ' ' -f 2
it also outputs 'bar'. The -s (for "squeeze") option of tr turns every sequence of the specified character (space in this case) into one instance of this character.

Of course, the awk solution is more succint and elegant in this case - I just think that tr -s / cut -d is handy to know from time to time, too.


The interested reader should not assume hastily, however, that tr -s is enough to make cut behave exactly like awk. Hint: leading spaces.

    pi:~$ echo ' foo' | tr -s ' ' | cut -d ' ' -f 1 
    
    pi:~$ echo ' foo' | awk '{print $1}'
    foo




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

Search: