Replace last occurrence of a string

Monday, June 17, 2013 0 Comments

How do you replace  instances of a string only in the line where they last occured.
for example if you have a file like below:
a
b
c
a
d
The output should look like below:
a
b
c
x
d
Below is the perl command that will perform the same:
perl -e '@a=reverse<>;
         END{
            for(@a){
             if(/a/){s/a/c/;last}
             }
            print reverse @a}' your_file

0 comments: