3 ms·
Just stumbled on this one recently, I forget where: foo | awk '{ print $NF }' That prints the last column. $NF = number of fields. I use it a lot like thi
by pjungwir 13y ago
Just stumbled on this one recently, I forget where:
foo | awk '{ print $NF }'
That prints the last column. $NF = number of fields. I use it a lot like this:
git status | grep deleted: | awk '{ print $NF }' | xargs foo
I actually just realized, though, that I don't understand why this works. It seems like it should be '{ print ${$NF} }' or something, and the command as-given should just print "3" or "6" or whatever. I'm not an awk expert, so I'd love to understand what's going on here.
- pbnjay 13y agoBare NF is number of fields, so the $ must be pulling the value first. Also, awk is 1-based, not 0, so you get the last field :)
- drothlis 13y agocat > asdf.txt <<EOF a b c d e f g h i j EOF cat asdf.txt | awk '{print $1}' cat asdf.txt | awk '{print $2}' cat asdf.txt | awk '{print $(3-1)}' cat asdf.txt | awk '{print NF}' cat asdf.txt | awk '{print $NF}'
- stevekemp 13y agoThis always bugs me: git status | grep deleted: | awk '{ print $NF }' | xargs foo Instead of using both grep and awk just use awk: git status | awk '/deleted:/ {print $NF}' | xargs foo