Philip Newton (pne) wrote,
Philip Newton

  • Mood:

grep | awk

Occasionally I see people writing little pipelines using both 'grep' and 'awk'... and it makes me wonder why you start two processes when one will do. (A bit like those who start pipelines with cat file | ... where <file ... will do just as well, and save one process.)

For example, why not replace ... | grep blurfle | awk '{ print $2 }' with ... | awk '/blurfle/ { print $2}'? Or replace ls -l | grep -v '^d' | awk '{total+=$5}END{print total}' with ls -l | awk '$1 !~ /^d/ {total+=$5} END {print total}'?

Especially since most of those recipes use a very simple argument to grep—typically a fixed string, or -v plus a fixed string—which would seem trivially convertible to part of an awk script even without having to learn the full awk language.

  • Post a new comment


    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded