Using awk
, I would like to print the last matching line of a file.
I would like only the matching line itself, not any range of lines.
I can use a command like this
awk '/foo/' bar.txt
However this will print all matching lines, not just the last one.
You can save the value in a variable and then print it after processing the whole file:
awk '/foo/ {a=$0} END{print a}' file
$ cat file
foo1
foo2
foo3
4
5
$ awk '/foo/ {a=$0} END{print a}' file
foo3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With