j4ck Jul 4, 2013 #1 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?
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?
lme@ Administrator Staff member Administrator Moderator Developer Jul 4, 2013 #2 I'd use awk(1) for this. Count the occurrences of foobar in a variable and sub() when the n'th occurrence is found.
I'd use awk(1) for this. Count the occurrences of foobar in a variable and sub() when the n'th occurrence is found.
OP j4ck Jul 4, 2013 Thread Starter #3 Check this: sed -e :a -e '$!N' -e '$!ba' -e 's/\(TEXT_TO_FIND\)/\1\nNEW_TEXT_TO_INSERT/Nth_OCCURANCE' inputfile
Check this: sed -e :a -e '$!N' -e '$!ba' -e 's/\(TEXT_TO_FIND\)/\1\nNEW_TEXT_TO_INSERT/Nth_OCCURANCE' inputfile