How do I use awk to find the last occurence of a string in a file?
grep string file | tail -1
If you insist on using awk:
awk '/string/{a=$0}END{print a}' file
1 Like
Hi, mirni:
To match an arbitrary string, it's best to stay away from regular expression syntax, since the string could include metacharacters. Tweaking your code slightly:
grep -F string file | tail -n1
awk -v s=string 'index(s, $0) {a=$0} END {print a}' file
Regards,
Alister
2 Likes
$ tac file | grep -m1 string
1 Like