Solved

Overwriting part of one file with part of another file using unix script or perl

Posted on 2010-11-09
3
319 Views
Last Modified: 2012-05-10
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


2.4583E+07
1.3956E+03
5.39583E+07
1.2583E+05
5.39583E+07
4.3933E+05
5.39583E+06
7.1183E+07
1.07917E+08
4.85625E+09
1.3E+08
4.3E+08
7.5E+33
2.3E+08
4.3E+07
3.2E+05
2.3E+07
5.3E+08
4.6E+06
2.7E+10

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.

  echo "19.39 -155.100 50.0 5.39583E+07 300.0" >>CONTROL
  echo "19.39 -155.101 100.0 5.39583E+07 300.0" >>CONTROL
  echo "19.39 -155.102 150.0 5.39583E+07 300.0" >>CONTROL
  echo "19.39 -155.103 170.0 5.39583E+07 300.0" >>CONTROL
  echo "19.39 -155.104 190.0 5.39583E+07 300.0" >>CONTROL
  echo "19.39 -155.105 210.0 5.39583E+07 300.0" >>CONTROL
  echo "19.39 -155.106 230.0 5.39583E+07 300.0" >>CONTROL
  echo "19.39 -155.107 250.0 5.39583E+07 300.0" >>CONTROL
  echo "19.39 -155.108 270.0 1.07917E+08 300.0" >>CONTROL
  echo "19.39 -155.109 300.0 4.85625E+09 300.0" >>CONTROL
  echo "19.401391 -155.280640 50.0 2.3E+08 100.0" >>CONTROL
  echo "19.401391 -155.280740 150.0 2.3E+08 100.0" >>CONTROL
  echo "19.401391 -155.280840 300.0 2.3E+08 100.0" >>CONTROL
  echo "19.401391 -155.280940 350.0 2.3E+08 100.0" >>CONTROL
  echo "19.401391 -155.281040 400.0 2.3E+08 100.0" >>CONTROL
  echo "19.401391 -155.281140 450.0 2.3E+08 100.0" >>CONTROL
  echo "19.401391 -155.281240 550.0 2.3E+08 100.0" >>CONTROL
  echo "19.401391 -155.281340 600.0 2.3E+08 100.0" >>CONTROL
  echo "19.401391 -155.281440 650.0 4.6E+08 100.0" >>CONTROL
  echo "19.401391 -155.281540 700.0 2.07E+10 100.0" >>CONTROL
file1.txt
file2.txt
0
Comment
Question by:libertyforall2
3 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
Comment Utility
#!/usr/bin/perl                                                                                    
open F1,"<file1.txt";
chomp (@f1=<F1>);
@ARGV=("file2.txt");
$^I=".bak";
while( <> ){
    s/([\d.]+E\+\d+)/shift @f1/e;
    print;
}
0
 

Author Closing Comment

by:libertyforall2
Comment Utility
Perfect! your a genius!!
0
 
LVL 2

Expert Comment

by:rocman
Comment Utility
I would use Perl as per attachment ...


#! perl
# Script arguments are NEW_DATA_FILE and FILE_TO_UPDATE

# Open new data file
open( 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 line
open( 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 file
open( OUTPUT_FH, '>' . $ARGV[1] ) ||
    die "Failed to open '$ARGV[1]' for overwrite: $!";

foreach ( @old_data ) {
    print( OUTPUT_FH $_ );
}

close( OUTPUT_FH );

Open in new window

0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now