Trying to modified files using sed command

hi all,

i will like to modified some files with the extension .gjf . All this files have in first line this #P PM3 Opt and i want to change that to this :

 %nproc=2    
 %chk=filename.chk 
 #p B3LYP /6-31G** opt

in order to do that i have try to do a script with the sed command but i really don't know how to insert %nproc=2 in line N�1, %chk=filename.chk in line N�2 and #p B3LYP /6-31G** opt in line N�3 . The script that i have wrote is

 for x in $(ls *.gjf);  
   do n=$(echo $x | cut -d. -f1);
  echo " sed -i 's/#P PM3 Opt/nproc=2 %chk=$n.chk #p B3LYP \/6-31G** opt/g' $n.gjf" 
 
 done

but this change the text #P PM3 Opt by nproc=2 %chk=$n.chk #p B3LYP / 6-31G** opt all this text in the first line.

i will be very grateful if some one of the forum can help me in solve this problem.

thank you very much .

sed -i 's/#P PM3 Opt/nproc=2\n%chk=$n.chk\n#p B3LYP \/6-31G** opt/' *.gjf

New lines are \n in the code above :slight_smile:

1 Like

first i will like to thanks for the very quick answer that you provide me however i have change the scrip to this :

  for x in $(ls *.gjf);  
    do n=$(echo $x | cut -d. -f1); 
   echo "  sed -i 's/#P PM3 Opt/nproc=2\n%chk=$n.chk\n#p B3LYP \/6-31G** opt/g' $n.gjf  "
  done

but this script do not modified any file also in the second line i need the name of the file with this particular extension %chk=$n.chk

thank you very much.

#!/bin/sh

for x in *gjf; do
    n=${x%.*}
    sed -i0 's/#P PM3 Opt/nproc=2\n%chk='"$n"'.chk\n#p B3LYP \/6-31G** opt/' "$x"
done

Putting "$n" out of sed script seems to do the trick :slight_smile:
[Wont edit this] I added 0 after -i in sed command so all gjf files will be backed up as .gjf0, in case something goes wrong

1 Like

thank you very much for all the help, and again for the quick answer