Skip to main content

Command Palette

Search for a command to run...

i’m begging you to stop doing grep | awk

Updated
2 min read
i’m begging you to stop doing grep | awk

i get it, pet peeves are inherently irrational. i would even go so far as to say that’s why we have that phrase for the concept, to illustrate to everyone involved that the thing we are about to rant about is not of life or death importance. so it is with that level of self-awareness that i say to you now something i first wrote down and published more than two decades ago:

for the love of all that is good in this world please, i’m begging you, stop doing this:

command | grep “string” | awk ‘{print $1}’

seriously.

ok sure, this is “old man yells at cloud” territory but you can just use awk for that and save yourself a pipe:

command | awk ‘/string/ {print $1}’

but what about case insensitive matching like grep -i i hear you ask? gnu awk has had that covered for longer than your career:

command | awk -v IGNORECASE=1 ‘/string/ {print $1}’

i know, sometimes it’s just easier to bang out a long string of grep "string" | grep -v "otherstring" when doing something in your homedir but if you put that into a published README or script i reserve the right to silently hate you forever. and also you can still do that with just awk.

command | awk -v IGNORECASE=1 ‘/string/ && !/otherstring/ {print $1}’

so yeah, stop that. thanks.