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

Why not `… | cut -f1 -d ” ”` though?


Because awk understands "columns" better.

echo '999 888' | awk '{print $2}'

prints 888


How is that different from

    echo '999 888' | cut -f2 -d " "
except for the default delimiter being space for awk?


   echo '999  888' | cut -f2 -d " " # notice two consecutive spaces
returns NULL.


There are invalid column separators for awk too. To handle consecutive spaces in cut you have squeeze them:

  echo '999  888' | tr -s " " | cut -f2 -d " "


Right, that would be expected though? I suppose awk is better for parsing formatted (padded) output.


How can it be "better" when it is exactly the same?


Speaking personally I find that I always use awk for printing (numbered) fields in shell-scripts, and when typing quick ad-hoc commands at the prompt.

The main reason for this is that it's easy to add a grep-like step there, without invoking another tool. So I can add a range-test, a literal-test, or something more complex than I could do with grep.

In short I can go from:

       awk '{ print $2 }'
To this, easily:

       awk '$1 == "en4:" {print $NF}'


Yet using cut, even with extra piping to tr or grep before, is probably over two times faster than awk, which may or may not matter depending on what you’re doing.




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

Search: