This might work for you:
sed '1,/MatchMeOnce/s//MATCHED/' file
This will work for all sed options if MatcMeOnce
is on the second line or higher, or this (GNU sed):
sed '0,/MatchMeOnce/s//MATCHED/' file
which satisfies the condition above the edge:
Or another alternative (all sed) that dumps the entire file into memory:
sed ':a;$!{N;ba};s/MatchMeOnce/MATCHED/' file
which has the additional advantage that if you want to select the nth and not the 1st from MatchMeOnce
, all you need to do is change the appearance flag, i.e. change second occurrence:
sed ':a;$!{N;ba};s/MatchMeOnce/MATCHED/2' file
To change the last use, use:
sed ':a;$!{N;ba};s/\(.*)MatchMeOnce/\1MATCHED/' file
source share