replace the last occurance of a string in a file

awk, regex, search, sed, shell

Solution

You can use tac with awk:

tac file | awk '!done && /Gottagohome saab 90 75n/{done=1; sub(/&$/, "")}1' | tac

Problem

Here's what my file looks like ``` Gottagohome saab 90 75n > homelog 2>&1& Gottagohome saab 90 75n > homelog 2>&1& Gottagohome saab 90 75n > homelog 2>&1& ``` Those commands are dynamically generated. What I want is to look for the last Command and make it run in the foreground like this ``` Gottagohome saab 90 75n > homelog 2>&1& Gottagohome saab 90 75n > homelog 2>&1& Gottagohome saab 90 75n > homelog 2>&1 ``` The & should be knocked off the last command. Any idea how this is done. actually I prolly need a wait inserted also before the last occurrence of gottogohome command. Could some one pl that one too . TY Sam

Original source