Looking for a method to insert a line after the Nth occurence of a given string.
The following is close to what I'm looking for, but is line number based, versus based on the Nth occurrence of a given string.
perl -ni -e 'print; print "Put after fifth line\n" if $. == 5' inFile.txt
The following will add a line of xyz
after the second occurrence of the string abc
:
perl -pi -e '/abc/&&++$n==2 and $_.="xyz\n"' inFile.txt
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With