LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Match and Print the results in different files (https://www.linuxquestions.org/questions/programming-9/match-and-print-the-results-in-different-files-750296/)

Priyabio 08-26-2009 02:06 AM

Match and Print the results in different files
 
Hi

I am having 2 txt file, one with 199 ids eg..00010,00051 etc and the second with some information regarding this ids...Now i wanna match this 199 ids one by one and print the matched lines in individual files ie. 199 files.

pls reply me asap

Disillusionist 08-26-2009 02:30 AM

Please provide a sample of the files content. And what you have tried so far.

I am assuming that the ID is contained in both files, if so you need to read the ID's from the first file into an array and scan the second file for each array entry.

XavierP 08-26-2009 03:26 AM

Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.

Priyabio 08-27-2009 03:36 AM

Hi

Below given is the model input files.

input 1


00010
00020
00030
00040
00051
00052
00053
00500
00530
00520
00620
00630
00640
00650
00562


input 2

Q5ST30 hsa00290 hsa00970 2 P42336 hsa00562 hsa04012 hsa04370 hsa04630 hsa04070 hsa04150 hsa04810 hsa04210 hsa04510 hsa04910 hsa04620 hsa04650 hsa04660 hsa04662 hsa04664 hsa04670 hsa05200 hsa05210 hsa05212 hsa05214 hsa05221 hsa05220 hsa05218 hsa05211 hsa05215 hsa05213 hsa05222 hsa05223 hsa04930 29

Disillusionist 08-27-2009 11:09 AM

There doesn't appear to be any corelation between the contents of file1 and file2.

Assuming that the files are "known" to contain good data (always a hell of an assumption) and the first file contains the IDs and the second contains the information about the IDs (in a 1-1 ratio, therefore your information must always be a single line)

You could read the first file into an array, read the second file into another array, loop through the array's and create your files...

I would suggest Perl for this, but I'm sure someone will have a way of doing something clever in Awk.

Perl Script:
Code:

#!/usr/bin/perl

##
## Open file1 and read contents into an array
##
open(FILE1, "file1") or die "Unable to open file1";
@Array1=<FILE1>;
close(FILE1);

##
## Open file2 and read contents into an array
##
open(FILE2, "file2") or die "Unable to open file2";
@Array2=<FILE2>;
close(FILE2);

##
## Find out how many elements in Array1
##
$l_array_size=@Array1;

##
## Set counter to zero
##
$l_count=0;

##
## Loop through Array elements
##
while ($l_count < $l_array_size)
{
  ###
  ### Remove Charage Return from Array Element
  ###
  chomp($Array1[$l_count]);
  ###
  ### Open Output File for this entry
  ###
  open(OUTPUT, " > output.$l_count");
  ###
  ### Print the Array Elements out to Output File
  ###
  print(OUTPUT "$Array1[$l_count] $Array2[$l_count]");
  ###
  ### Close Output File
  ###
  close(OUTPUT);
  ###
  ### Increase counter
  ###
  $l_count++;
}



All times are GMT -5. The time now is 09:29 AM.