-2

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

my file is like this

%nproc=1
%chk=pes.chk
#CCSD/6-31G* Opt=ModRedundant MaxDisk=50GB

Title

-1 1
H xxxx xxxx xxxx

I want to change it to this,

%nproc=20 
%chk=pes.chk 
%mem=200GB
#CCSD/6-31G(d) Opt SCF(maxcyc=1000)

Title

-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

1

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
0

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 .