Insert row without deleting previous data using sed

Hello,

I want to add a new row to a file to insert data without deleting the previous data there.

Example:

file

a
b
c
d

Output

a
b
newtext
c
d

I want to be able to choose any row I want to change and without deleting anything else. basically i want to add the new row and move everything else down so I'm able to add data to the new row.

this is what i have tried so far

sed '3s/^/newtext\n/' file > Output

Try doing this

sed '3inewtext' file > output

Yea I was trying that also but it gives a warning:

command i expects \ followed by text

---------- Post updated at 06:13 PM ---------- Previous update was at 06:04 PM ----------

I tried something different here is the code but

sed '3 s/^/newtext\n/' file > Output

but it gives this

a
b
newtextnc
d

note the "n" between newtext and c

I want

a
b
newtext
c
d

---------- Post updated at 06:47 PM ---------- Previous update was at 06:13 PM ----------

I can't make c to go to the next row. after replacing newtext

umm my solution seems to work fine with GNU sed, try this:

sed '3i\
test' infile > outfile

i get the error message

extra characters after \ at the end of i command

it could be that Im using a different version or something. =/ any way of knowing that?

or is it possible to use awk? I don't want to open my 4 GB data file and go to the x row and enter the data manually. Opening the data file that big will take forever to open or sometimes it will not open on my computer. =/

Try this...

awk 'NR==3{print "newtext"}1' input_file > out_file

If solaris, use nawk!

--ahamed

1 Like

Chubler thanks I guess it didn't work for me. Thank you for your time.

And thanks Ahamed it works fine. Thanks