Given a file like this:
a b a b
I\'d like to be able to use sed to replace just the last line that contains an instance of \"a
sed
Given:
$ cat file a b a b
You can use POSIX grep to count the matches:
grep
$ grep -c '^a' file 2
Then feed that number into awk to print a replacement:
awk
$ awk -v last=$(grep -c '^a' file) '/^a/ && ++cnt==last{ print "c"; next } 1' file a b c b