AWK - Display all fields except the last
awk 'BEGIN{FS=OFS="-"}{NF--; print}'
It's come up a few times where I wanted to get all fields, except for the last one, and I always had to google it, which would take me about 15 min or so to find a good example that I actually liked... Anyways, I just wanted to post it here so I can just come to it, and hopefully help the next person...
My example was, I wanted to get all the BuildConfig that are either, Complete|Failed|Cancelled, so that I can delete them/clean them up, since I didn't want to clean up the ones that were still building...
Getting the list was easy,
$ oc get build | awk '/Complete|Failed|Cancelled/ {print $1}'
example-1
example-2
another-build-1
another-build-2
build-1
But if I pipe that to:
awk 'BEGIN{FS=OFS="-"}{NF--; print}'
Now I get
$ oc get build | awk '/Complete|Failed|Cancelled/ {print $1}' | awk 'BEGIN{FS=OFS="-"}{NF--; print}'
example
example
another-build
another-build
build
and now I just pass it to 'uniq' and I get a list that I can use to clean up
$ oc get build | awk '/Complete|Failed|Cancelled/ {print $1}' | awk 'BEGIN{FS=OFS="-"}{NF--; print}' | uniq
example
another-build
build