Insert some text after Nth matching pattern using sed

Hi,

How can I do that? I have multiple, e.g. foobar, patterns in my file, how can I add some_text after e.g. the 4fourth one?
 
I'd use awk(1) for this. Count the occurrences of foobar in a variable and sub() when the n'th occurrence is found.
 
Check this:
sed -e :a -e '$!N' -e '$!ba' -e 's/\(TEXT_TO_FIND\)/\1\nNEW_TEXT_TO_INSERT/Nth_OCCURANCE' inputfile
 
Back
Top