I want to insert a string after every 30 lines in my large file. I'm using mini-sed, which doesn't support ~ (tilde) range operator. I'm looking for sed-only solution please.
You have to use the “-i” option with the “sed” command to insert the new line permanently in the file if the matching pattern exists in the file.
You can tell sed to carry out multiple operations by just repeating -e (or -f if your script is in a file). sed -i -e 's/a/b/g' -e 's/b/d/g' file makes both changes in the single file named file , in-place.
@JM88, Unix/Linux use the line-feed character ( \n ) for line breaks. Mac uses the carriage-return character for line breaks ( \r ), and Windows uses a combination of the two ( \r\n ) for line breaks.
This thread is another example of how to over complicate things. This should do it:
sed '0~30 s/$/string/g' < inputfile > outputfile
Every 30 lines "string" is inserted at the end of the line. If you want a new line with the word "string" just use "\n string".
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