Reputation: 927
I have a script which returns few lines of output and I am trying to print the last two words of the last line (irrespective of number of lines in the output)
$ ./test.sh
service is running..
check are getting done
status is now open..
the test is passed
I tried running as below but it prints last word of each line.
$ ./test.sh | awk '{ print $NF }'
running..
done
open..
passed
how do I print the last two words "is passed" using awk
or sed
?
Upvotes: 1
Views: 7073
Reputation: 203229
For robustness if your last line can only contain 1 field and your awk doesn't retain the field values in the END section:
awk '{split($0,a)} END{print (NF>1?a[NF-1]OFS:"") a[NF]}'
Upvotes: 1
Reputation: 58371
This might work for you (GNU sed):
sed '$s/.*\(\<..*\<.*\)/\1/p;d' file
This deletes all lines in the file but on the last line it replaces all words by the last two words and prints them if successful.
Upvotes: 0
Reputation: 289535
Just say:
awk 'END {print $(NF-1), $NF}'
"normal" awk
s store the last line (but not all of them!), so that it is still accessible by the time you reach the END
block.
Then, it is a matter of printing the penultimate and the last one. This can be done using the NF-1
and NF
trick.
Upvotes: 4