Trying to modified files using sed command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trying to modified files using sed command
# 1  
Old 12-17-2010
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 :
Code:
 %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
Code:
 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 .

Last edited by Scott; 12-17-2010 at 02:10 PM.. Reason: Please use code tags
# 2  
Old 12-17-2010
Code:
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 Smilie
This User Gave Thanks to tukuyomi For This Post:
# 3  
Old 12-17-2010
first i will like to thanks for the very quick answer that you provide me however i have change the scrip to this :
Code:
  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.

Last edited by Scott; 12-17-2010 at 02:45 PM.. Reason: Code tags, please...
# 4  
Old 12-17-2010
Code:
#!/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 Smilie
[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

Last edited by tukuyomi; 12-17-2010 at 03:25 PM.. Reason: Removed "Debug" lines
This User Gave Thanks to tukuyomi For This Post:
# 5  
Old 12-17-2010
thank you very much for all the help, and again for the quick answer
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Du command and modified date in ssh

Hello, I am a new user to linux and having trouble completing this task. I want to list a directory of files and folders including the name, human readable size, and last modified date. So far I can get two of the three but not all together. For example: Using ls -lh gives me drwxr-xr-x... (7 Replies)
Discussion started by: Newuzer
7 Replies

2. UNIX for Dummies Questions & Answers

Copy files from one drive to another, keeping most recently modified files

Hi all, I am a bit of a beginner with shell scripting.. What I want to do is merge two drives, for example moving all data from X to Y. If a file in X doesn't exist in Y, it will be moved there. If a file in X also exists in Y, the most recently modified file will be moved to (or kept) in... (5 Replies)
Discussion started by: apocolapse
5 Replies

3. UNIX for Dummies Questions & Answers

Find command to get the modified files past 2 hours

Hello, How to get the modified/created files past 2 hours in Solaris with find command? Thank you. (7 Replies)
Discussion started by: balareddy
7 Replies

4. UNIX for Dummies Questions & Answers

Finding the modified timestamp of files from the piped output of du command

Version Info +++++++++++++++ RHEL 5.4 Since ls command lists file sizes in Bytes which can be long I use du command like below. I have run the du command for the below files as shown below. But I want pipe this output to ls command just to see the modified timestamp for these files. ... (7 Replies)
Discussion started by: kraljic
7 Replies

5. Shell Programming and Scripting

command to know last modified user of a file

Hi I have below requirement. There are set of files to be monitored for audit purpose. Source file contains the file name and its location on server. Need to have shell script which generate a output file which is having the details of above files ( last modified user name ,lat update... (12 Replies)
Discussion started by: karnatis
12 Replies

6. UNIX for Advanced & Expert Users

command for recently modified files - "find" command not working

I have three files a.txt , b.txt , c.txt in a directory called my_dir1 .These files were created before two or three months . I have a tar file called my_tar1.tar which contains three files a.txt , b.txt , d.txt . Somebody untarred the my_tar1.tar into my_dir1 directory. So existing two files were... (1 Reply)
Discussion started by: joe.mani
1 Replies

7. Shell Programming and Scripting

command to know files modified morethan 30 min

Hi, i use ksh and want to know the command for gettting the files which were not modified in last 30 min. find . -name <filename > -mtime 0.0209 is not giving the results. Thanks , Mohan (3 Replies)
Discussion started by: mohanpadamata
3 Replies

8. UNIX for Dummies Questions & Answers

Help - Find command with list of files modified descending Order

Hi, I would like to know the command to get the files order in descending order with "FIND" command. Appreciate your help Thanks (4 Replies)
Discussion started by: TonySolarisAdmi
4 Replies

9. UNIX for Dummies Questions & Answers

how to retrieve original contents of a modified file (modified using vi)

Made changes to a file using vi editor and saved those changes now realised that the changes are not required How can I get the previous version of the file.i.e the one which was there on which I had made changes (3 Replies)
Discussion started by: novice100
3 Replies

10. UNIX for Dummies Questions & Answers

chmod command for recently modified files

hello! is there a way for me to use the chmod command to change permissions for several files all at once -based on the fact that these files were all most recently modified TODAY ? I can't use a wildcard on their filenames because the filenames are varied. But I was hoping I could somehow do... (2 Replies)
Discussion started by: polka_friend
2 Replies
Login or Register to Ask a Question