Last Updated: February 25, 2016
·
1.79K
· bendihossan

Stop piping grep to awk, just use nawk

Say I want to generate a list of modified files in my current git repo without all the extra gubbins that git outputs.

git status | grep 'modified'

outputs:

#   modified:   Entity/Profile/Achievement.php
#   modified:   Entity/Profile/Activity.php
#   modified:   Entity/Profile/Category.php

or:

git status | grep 'modified' | awk '{ print $3 }'

outputs:

Entity/Profile/Achievement.php
Entity/Profile/Activity.php
Entity/Profile/Category.php

The above output is much nicer, but there's an even shorter way of achieving the same thing using nawk because this command can do the work of grep and awk in one go!

git status | nawk '/modified/ { print $3 }'

outputs:

Entity/Profile/Achievement.php
Entity/Profile/Activity.php
Entity/Profile/Category.php

Pretty basic example but now I have one less command to try and remember the syntax of :)

1 Response
Add your response

Do not use git status without --porcelain flag in your scripts. My version using "normal" AWK

git status --porcelain | awk '/^ M/ { print $2 }'
over 1 year ago ·