Overwriting part of one file with part of another file using unix script or perl
Ok. I have one file that is updated with new numbers and I need to copy the data from file 1 and replace part of file 2 with the new numbers in file 1. I have attached file1 & file2 in their entirety. Basically if I have this below
The script needs to search for the part of the script below then replace only the data in column 5 (I believe its 5 but perhaps the script should check for the first characters that contain E+) with the new data above but only the portion above. Nothing else needs to be changed.
#! perl# Script arguments are NEW_DATA_FILE and FILE_TO_UPDATE# Open new data fileopen( INPUT_FH, $ARGV[0] ) || die "Failed to open '$ARGV[0]' for reading: $!";my @new_data = <INPUT_FH>;close( INPUT_FH );chomp( @new_data);# Open existing data file and process each lineopen( INPUT_FH, $ARGV[1] ) || die "Failed to open '$ARGV[1]' for reading: $!";my @old_data = <INPUT_FH>;close( INPUT_FH );my $data_idx = 0;foreach ( @old_data ) { # Replace the measurement s/(.*)(\s+)([\d.]+E\+[\d]+)(\s.*)/$1$2${new_data[$data_idx++]}$4/ unless $data_idx == scalar(@new_data);}# Rewrite existing data fileopen( OUTPUT_FH, '>' . $ARGV[1] ) || die "Failed to open '$ARGV[1]' for overwrite: $!";foreach ( @old_data ) { print( OUTPUT_FH $_ );}close( OUTPUT_FH );