How to use awk to replace portions of a file with another file

Dear All,

Please I want the contents of the file "myTempMechRates.txt" to replace a portion of the file "tempRates.txt". The contents of myTempMechRates.txt is:
4.10E+06   5.00     -298.0
3.10E+06   2.00     1450.0
8.14E+06   9.00     658.0

The contents of tempRates.txt is:
C3H6+OH = aC3H5+H2O                          3.10E+06   2.00     -298.0    !91TSA 
 C3H6+OH = CH3CCH2+H2O                        1.10E+06   2.00     1450.0    !91TSA 
 C3H6+OH = CH3CHCH+H2O                        2.14E+06   2.00     2778.0    !91TSA

I want the following portion of tempRates.txt :
3.10E+06   2.00     -298.0 
1.10E+06   2.00     1450.0
2.14E+06   2.00     2778.0
to be replaced by the contents of myTempMechRates.txt so that it will become:
C3H6+OH = aC3H5+H2O                          4.10E+06   5.00     -298.0    !91TSA 
 C3H6+OH = CH3CCH2+H2O                        3.10E+06   2.00     1450.0   !91TSA 
 C3H6+OH = CH3CHCH+H2O                        8.14E+06   9.00     658.0    !91TSA

This is the code I have and I am not getting the correct result:
awk 'NR==FNR{a[NR]=$1;next}{$4=a[FNR]}1' "C:\Users\gotenggl\CHEMKINMATLAB2\
Working_Dir\myTempMechRates.txt" "C:\Users\gotenggl\CHEMKINMATLAB2\Working_Dir\tempRates.txt"

C3H6+OH = aC3H5+H2O 4.10E+06 2.00 -298.0 !91TSA
C3H6+OH = CH3CCH2+H2O 3.10E+06 2.00 1450.0 !91TSA
C3H6+OH = CH3CHCH+H2O 2.14E+06 2.00 2778.0 !91TSA

I am using a Windows terminal with Linux/shell capabilities. Any help is appreciated:

Kind regards
Gokop

Comments

  • hi,
    NR==FNR{a[NR]=$0;next}...
    
    $0 is the whole record, $1 is the first field of the record, and so on.
  • Thank you so much, it works after using $0 and changing $4 to $7 and the last $1 to $7 too which becomes:
    awk 'NR==FNR{a[NR]=$0;next}{$7=a[FNR]}7' .....
    
Sign In or Register to comment.