PDA

View Full Version : Replacing one line in text file with line from other file



abraxas334
February 7th, 2013, 02:24 PM
I have a question regarding replacing line in files.
I want to replace the ith line of replace.dat with the jth line of othertext.dat how can i achieve this?

Vaphell
February 7th, 2013, 04:10 PM
i=2; j=2
sed "${i}s/.*/$( sed -n ${j}p overtext.txt)/" replace.dat

you can plug numbers directly in ${i} and ${j}'s place
by default sed prints to screen, if you want to save the changes either redirect output to new file with > output.file (non-destructive=safer) or with -i parameter given to main sed command (overwrites source file, so better be sure it works first)

ofnuts
February 7th, 2013, 04:43 PM
head -$((i-1)) replace.dat > out.dat
head -$j othertext.dat | tail -1 >>out.dat
tail -n +$((i+1)) replace.dat >>out.dat

abraxas334
February 8th, 2013, 03:50 PM
Thanks for all the replies! This helped alot!