Script to append text from one file into another


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to append text from one file into another
# 1  
Old 07-05-2010
Question Script to append text from one file into another

Hello all.

I was wondering if it possible to write a bash script that would do the following:

I perform molecular modelling calculations and the output files are all text files with various different extensions.

For example, I submit the input file "job_name.inp" and when it is done or the 2 day time is reached, I get an output file "job_name.inp". A typical input file looks something like this:

Code:
 $CONTRL SCFTYP=RHF RUNTYP=OPTIMIZE DFTTYP=M06-L MAXIT=199 MULT=1 NOSYM=1
  $END
 $SYSTEM TIMLIM=2850 MWORDS=50 PARALL=.TRUE. $END
 $SCF DIRSCF=.TRUE. FDIFF=.f. $END
 $STATPT OPTTOL=0.0001 NSTEP=500 HSSEND=.t. $END
 $FORCE TEMP=298.15 $END
 $DATA
 N2
C1
NITROGEN    7.0      4.7543100000     15.8608300000     13.1479200000
S   6
  1   4173.5110000              0.0018348        
  2    627.4579000              0.0139950        
  3    142.9021000              0.0685870        
  4     40.2343300              0.2322410        
  5     12.8202100              0.4690700        
  6      4.3904370              0.3604550        
L   3
  1     11.6263580             -0.1149610              0.0675800        
  2      2.7162800             -0.1691180              0.3239070        
  3      0.7722180              1.1458520              0.7408950        
L   1
  1      0.2120313              1.0000000              1.0000000        
D   1
  1      0.8000000              1.0000000       
 
NITROGEN    7.0      4.2234914760     16.5243904163     13.8182793318
S   6
  1   4173.5110000              0.0018348        
  2    627.4579000              0.0139950        
  3    142.9021000              0.0685870        
  4     40.2343300              0.2322410        
  5     12.8202100              0.4690700        
  6      4.3904370              0.3604550        
L   3
  1     11.6263580             -0.1149610              0.0675800        
  2      2.7162800             -0.1691180              0.3239070        
  3      0.7722180              1.1458520              0.7408950        
L   1
  1      0.2120313              1.0000000              1.0000000        
D   1
  1      0.8000000              1.0000000       
 
 $END

If the job does not finish in the allotted 2 days, then another file is created "job_name.rst", which contains data that is required to restart the calculation.

If I want to restart the calculation from where it was killed by the job-scheduler, then I need to make a new input file, let's call it "job_name_restart.inp". At the very end of the text within this new input file "job_name_restart.inp" I have to paste in text that is contained in "job_name.rst".

A shortened version of the "job_name.rst" file looks like this:

Code:
ENERGY/GRADIENT/DIPOLE RESTART DATA FOR RUNTYP=HESSIAN
 butadiene_near_S_of_MO_TFD2_S_attempted_concerted_TS                           
 $VIB   
         IVIB=   0 IATOM=   0 ICOORD=   0 E=    -3717.1435124522
-5.165258381E-04 1.584665821E-02-1.206270555E-02-2.241461728E-03 3.176050715E-03
-5.706738823E-04 2.502034151E-03 5.130112290E-04-2.716945939E-03 1.357008279E-03
-1.059915305E-03 1.693526456E-03-2.957638907E-04-5.994938737E-04 9.684054361E-04
.
.
.
.

The text eventually ends with one blank line. What I need is the $VIB heading and all of the text after $VIB to be appended to the end of file "job_name_restart.inp" and then to have "$END" inserted at the very end of the file.

I should also mention that the "job_name.rst" file is always placed in the directory /scratch/mzd/gamess-scratch/.

The input file is always in a separate directory with a different descriptive name of the job I am running. In this example, it would be /scratch/mzd/job_name/.

Any help would be tremendously appreciated.
# 2  
Old 07-05-2010
Check if this provides desired output:
Code:
awk '/^\$VIB/{p=1}p;END{print "$END"}' job_name.rst

If it does then this will append output to job_name_restart.inp:
Code:
awk '/^\$VIB/{p=1}p;END{print "$END"}' job_name.rst >> job_name_restart.inp

# 3  
Old 07-05-2010
I am sorry, but I am not very good at this.

What is your first suggestion supposed to do? What changes am I looking for?

I typed in, and all it did was print "$END" below my command line.
# 4  
Old 07-05-2010
Sorry, I didn't notice space in front of "$VIB". Try now:
Code:
awk '/\$VIB/{p=1}p;END{print "$END"}' job_name.rst

This should print on your terminal lines that should go to job_name_restart.inp. If they are correct, then redirect that command's output using ">>":
Code:
awk '/\$VIB/{p=1}p;END{print "$END"}' job_name.rst >> job_name_restart.inp

# 5  
Old 07-05-2010
Hey there. Thanks so much for replying so quickly. But I get the same thing, although it seemed to pause for a second before it showed the "$END" in the terminal window.

---------- Post updated at 04:02 PM ---------- Previous update was at 03:58 PM ----------

OK. I entered in the second command that you suggested, and all it did was add "$END" to the file. The text from the file "job_name.rst" was not appended.
# 6  
Old 07-05-2010
Are you sure there is line containing "$VIB" in file job_name.rst? And did you copy that code correctly? There is no point in running second line of code, if first one doesn't provide desired output.
# 7  
Old 07-05-2010
I just checked two or three different .rst files and there is always a line containing "$VIB".

It is always on the 3rd line of the file, and it is always preceded by 1 space. It is followed by three spaces and then a new line starts. I don't know if this helps but I will represent spaces with underscores: This is how the 3rd line of the the job_name.rst file:

"_$VIB___"

If it helps, "$VIB" starts on column 2 and the cursor moves to the next line when it reaches column 9 i.e. there is no column 10 on that line.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX script to append multiple text files into one file based on pattern present in filaname

Hi All-I am new to Unix , I need to write a script. Can someone help me with a requirement where I have list of files in a directory, I want to Merge the files if a pattern of string matches in filenames? AAAL_555A_ORANGE1_F190404.TXT AAAL_555A_ORANGE2_F190404.TXT AAAL_555A_ORANGE3_F190404.TXT... (6 Replies)
Discussion started by: Shankar455
6 Replies

2. UNIX for Beginners Questions & Answers

awk to update file with partial matching line in another file and append text

In the awk below I am trying to cp and paste each matching line in f2 to $3 in f1 if $2 of f1 is in the line in f2 somewhere. There will always be a match (usually more then 1) and my actual data is much larger (several hundreds of lines) in both f1 and f2. When the line in f2 is pasted to $3 in... (4 Replies)
Discussion started by: cmccabe
4 Replies

3. Shell Programming and Scripting

Need comand or script for append text after searching for the desired string

Hi all, i have a generated report in unix in the following command like input.txt 47.85,10 0124,42.35,8 0125,3.5,2 the input file format is fixed I need the my output file with append text as below output.txt 0124 amount:42.35 0125 amount:3.5 0124 count : 8 0125... (34 Replies)
Discussion started by: hemanthsaikumar
34 Replies

4. Shell Programming and Scripting

Needed shell script to append desired text to each line in a file

Hi, I had generated a report in my tool as followsoutput.txt 43.35 9 i needed the script to generate a new file like below i want to append the text to each of these lines of my filenewoutputfile.txt should be Total Amount : 43.35 Record Count:9 Regards, Vasa Saikumar. ... (2 Replies)
Discussion started by: hemanthsaikumar
2 Replies

5. Shell Programming and Scripting

How to append a text file?

i have to append a text file grep for a word, if found, put comment in starting of the line. here is an example cat test.sh bin/ksh Hello World Test Message :wq! search for "bin" word in test.sh file if found comment it out at starting of the line: Output as follows: #bin/ksh... (5 Replies)
Discussion started by: raghur77
5 Replies

6. Shell Programming and Scripting

Open the file and edit/append the text

Hi i have a file like this mailboxnum 20 filename <to subsitute> fileloaction /home/dd234/ filetype txt in a directory i have set of files for ex: TT45.1.2 TT45.1.3 TT45.1.4 . . in for loop i have to take this files and subsitute one by one ex: (1 Reply)
Discussion started by: greenworld123
1 Replies

7. Shell Programming and Scripting

Append Text in Result File Name

Hi Below command is returning the list of files which having this string "MTL_SYSTEM_ITEMS". find . -name "*"|xargs grep -il MTL_SYSTEM_ITEMS Ex: Above command is returing 2 files (Out of 10 files 2 files having this string). ./file1.txt and ./file2.txt Here I want to append... (3 Replies)
Discussion started by: balajiora
3 Replies

8. Shell Programming and Scripting

How to append text to the second line of a file

Say I have a text file like: 1 3 4 How would I use ksh to put the number '2' into the second line of that file? I'm using OpenBSD so the sed syntax might be a bit different (I have no idea how to use sed, though) (4 Replies)
Discussion started by: guitarscn
4 Replies

9. UNIX for Dummies Questions & Answers

append a text to a file every month

i have something like below in my SAS code and every month i need to append a text say 'ext.hlc_sum0906' near ext.hlc_sum0905 and next month after ext.hlc_sum0906 i need to append this 'ext.hlc_sum0907' and so on like that.. is it possible using SED or some other command in unix? %let... (1 Reply)
Discussion started by: depakjan
1 Replies

10. Shell Programming and Scripting

how to append text into a file.

I have a command stream that will parse down an ftp DIR listing of a remote directory and return the name of the newest file that I am interested in. The command is sed -e '/^d/d' sppay.listing |sed -n -e '/SPPAY/p'|sort -r -k 43M,45 -k 47,48 -k 50,54|sed -n -e '1p'|cut -c 56-99 and what it... (2 Replies)
Discussion started by: beilstwh
2 Replies
Login or Register to Ask a Question