Given a text file, how do you add a line of text after a specific line number? I believe I would want to use "sed" but I am unsure of the syntax. Thank you.
Mike
Given a text file, how do you add a line of text after a specific line number? I believe I would want to use "sed" but I am unsure of the syntax. Thank you.
Mike
suppose you want to add "hi there" between lines 2 & 3
sed '3i\
hi there' oldfile > newfile
after the \ there must a be a new line
Him Jim,
I really appreciate your response. The code you provided isn't doing quite what I would like. I have a text file called original.txt:
MIke$ cat original.txt
blah
blah
blah
blah
blah
MIke$ sed '3i\
> hi there' original.txt > new.txt
MIke$ cat new.txt
blah
blah
hi thereblah
blah
blah
I would like the output to be:
blah
blah
hi there
blah
blah
blah
Is there a way I can do that? Thanks again.
Mike
sed '3i\
hi there
' original.txt > new.txt
OR
sed "$(printf "3i%c\nhi there\n" '\')" original.txt > new.txt
Yes thank you very much. That works. Is there any way those commands could be combined into one line? For example:
sed '3i\; hi there;' original.txt > new.txt
That doesn't work but perhaps there is something similar? Thank you very much.
Mike
The second option is a one-liner.