Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

read and paste two files

Status
Not open for further replies.

darkknight18

Programmer
Jul 28, 2011
3
US
I need help in reading and pasting two files.
File list 1 - list of files ex: d3jf_fh_xyz and so on
File list 2 - list of files ex: d3jf_fh_abc

each file has one line of numbers separated by spaces. If the beginning part of the file name in list 1 matches file name in list 2 (i.e d3jf_fh), I want to paste the two lines into one output file for all files.

Since I am new to Perl, suggestions would be appreciated
 
I've written the following, it is very crude but works till the last step, where it has to paste two lines only if their ID's are the same.





#!/usr/bin/perl -w

use warnings;

######## READING FILE ONE ##########
$[ = 1;
open(DATA, "< list1") or die "Failed to open list1";
open(MYOUT1, "> out1") or die "Failed to open file1";
#print MYOUT "aa bb cc\n";
while (<DATA>){
@lines1 = <DATA>;
foreach $line1 (@lines1){
open (DATA1, "<$line1") or die "Failed to open $line1\n";
while (<DATA1>){
if ($. > 0){
chomp;
$ID1 = substr($line1, 1, 7);
open (FILE, "$line1");
@FId = split(' ', $_, -1);
$cor = $FId[1];
$icor = $FId[2];
$ocor = $FId[3];

}
}
printf MYOUT1 "$ID1 $cor $icor $ocor\n" ;
}
}
close (DATA);
close (DATA1);
close (FILE);
close (MYOUT1);

####### READING FILE TWO ##########

open(DATA, "< list2" ) or die "Failed to open list1";
open(MYOUT2, "> out2") or die "Failed to open file2";
while (<DATA>){
@lines2 = <DATA>;
foreach $line2 (@lines2){
open (DATA1, "<$line2") or die "Failed to open $line2\n";
while (<DATA1>){
if ($. > 0){
chomp;
$ID1 = substr($line2, 1, 7);
open (FILE, "$line2");
@FId = split(' ', $_, -1);
$cor = $FId[1];
$icor = $FId[2];
$ocor = $FId[3];
}
}
printf MYOUT2 "$ID1 $cor $icor $ocor\n" ;
}
}

close (DATA);
close (DATA1);
close (FILE);
close (MYOUT2);

######### PASTING THE TWO FILES TOGETHER ######

open(VCOR, "< out1" ) or die "Failed to open vcor";
open(DAT, "< out2" ) or die "Failed to open data";
open(MYOUT, "> megafitfile") or die "Failed to open ";
while (<VCOR>){
if ($.> 0){
chomp;
@FId = split(' ', $_, -1);
$a=$FId[1];
$b=$FId[2] ;
$c=$FId[3] ;
}
while (<DAT>){
if ($.> 0){
chomp;
@FId = split(' ', $_, -1);
$d=$FId[1];
$e=$FId[2] ;
$f=$FId[3] ;

}
}
#### THIS PART DOES NOT WORK ######

if ($a eq $d){
print MYOUT "$a $b $c $d $e $f\n" ;
}
else {print MYOUT "$a $b $c\n";}

}

close(VCOR);
close(DAT);
close(MYOUT);

 
You need to rewind the second file each time you read a line from the first one, otherwise the second file is read only once. I assume of course that you need to fully check the second file against every line read from the first one.
Add the line
[tt]seek DAT,0,0;[/tt]
before
[tt]while(<DAT){[/tt]
Also note that it is deprecated to use [tt]$a[/tt] and [tt]$b[/tt] as variable names.
And please provide a properly formatted code putting it between [ignore]
Code:
[/ignore] tags:
[tt][ignore][code][/ignore][i]your code here...[/i][ignore]
[/ignore][/tt]

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
Thanks prex1,

That works but I get a few errors such as

Use of uninitialized value $_ in scalar chomp at ./trial line 11, <DATA> line 5.
readline() on closed filehandle FILE at ./trial line 14
Use of uninitialized value $ID1 in string eq at ./trial line 64, <DAT> line 1


The files I'm using are :

list1
1aw7_AB.fit => 1 2 3
1bjw_AB.fit
1biq_AB.fit
1bsr_AB.fit

list2
1aw7_AB.vcor => 4 5 6
1bjw_AB.vcor
1biq_AB.vcor

with each file in the list containing a line of numbers.

So I want the output to be 1aw7_AB 1 2 3 1aw7_AB 4 5 6 and so on

The problem with the script is that the else command does not work.
Here's my code

Code:
#!/usr/bin/perl -w

use warnings;

######## READING FILE ONE ##########
$[ = 1;
open(DATA, "<list1") ;
open(MYOUT1, "> out1") ;
  @lines1 = <DATA>;
  foreach $line1 (@lines1){
    chomp;
    $ID1 = substr($line1, 1, 7);
    open (FILE, "<$line1");
      while(<FILE>){
      chomp;
     ($line2)=$_;

   }

printf MYOUT1 "$ID1 $line2\n" ;
  }
close (DATA);
close (FILE);
close (MYOUT1);
  
####### READING FILE TWO ##########

open(DATA, "< list2" ) or die "Failed to open list1";
open(MYOUT2, "> out2") or die "Failed to open file2";
  @lines2 = <DATA>;
  foreach $line2 (@lines2){
   chomp;
   $ID1 = substr($line2, 1, 7);
   open (FILE, "<$line2");
       while(<FILE>){
       chomp;
       ($line2)=$_;
  }

printf MYOUT2 "$ID1 $line2\n" ;
 }
 close (DATA);
 close (FILE);
 close (MYOUT2);
 
########## PASTING THE TWO FILES TOGETHER IF IDs MATCH ######

open(VCOR, "< out1" ) or die "Failed to open vcor";
open(DAT, "< out2" ) or die "Failed to open data";
open(MYOUT, "> megafitfile") or die "Failed to open ";
while (<VCOR>){
   chomp;
   ($line3)=$_;
   @FId = split(' ', $_, -1);
   $ID1=$FId[1];
seek DAT,0,0;

while (<DAT>){
    chomp;
    @FId = split(' ', $_, -1);
    $ID2=$FId[1];
    ($line4)=$_;

if ($ID1 eq $ID2){
print MYOUT  "$line3 $line4\n" ;
else {print MYOUT "$line3\n";} 
}
}
}

close(VCOR);
close(DAT);
close(MYOUT);
 
I'd do this in shell:

Code:
for fit in $(<list1) ; do vcor=${fit%.fit}.vcor ; if grep -qx $vcor list2 ; then echo ${fit%.fit} $(cat $fit) ${fit%.fit} $(cat $vcor) ; fi ; done

You could of course implement the same algorithm in perl.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top