I need to change this multiline but I'm confused about how to do deal with new lines,

my file is like this

#CCSD/6-31G* Opt=ModRedundant MaxDisk=50GB


-1 1
H xxxx xxxx xxxx

I want to change it to this,

#CCSD/6-31G(d) Opt SCF(maxcyc=1000)


-1 1 
H xxxx xxxx xxxx

any simple way to replace the first 3 lines with the new 4 lines? also, the \n before "Title" is important.

2 Answers 2


With GNU sed:

printf '%s\n' '%nproc=20' '%chk=pes.chk' '%mem=200GB' '#CCSD/6-31G(d) Opt SCF(maxcyc=1000)' |
  sed -e '3r/dev/stdin' -e '1,3d' file

or if the new lines are in a file named linesfile:

sed -e '3rlinesfile' -e '1,3d' file

Assuming file1 is the file to modify (to replace first 3 lines with a block of 4 lines) and you have copied the 4 new lines into file2, I would do this:

tail -n +4 file1 >> file2 && mv file2 file1

This is also efficient in case of a large file1, comparing to in-place editing commands.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .