LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-26-2009, 02:06 AM   #1
Priyabio
LQ Newbie
 
Registered: Aug 2009
Posts: 9

Rep: Reputation: 0
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
 
Old 08-26-2009, 02:30 AM   #2
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,013

Rep: Reputation: 83
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.
 
Old 08-26-2009, 03:26 AM   #3
XavierP
Moderator
 
Registered: Nov 2002
Location: Kent, England
Distribution: Lubuntu
Posts: 19,176
Blog Entries: 4

Rep: Reputation: 430Reputation: 430Reputation: 430Reputation: 430Reputation: 430
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 08-27-2009, 03:36 AM   #4
Priyabio
LQ Newbie
 
Registered: Aug 2009
Posts: 9

Original Poster
Rep: Reputation: 0
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
 
Old 08-27-2009, 11:09 AM   #5
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,013

Rep: Reputation: 83
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++;
}

Last edited by Disillusionist; 08-27-2009 at 11:48 AM. Reason: Added sample Perl script
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I print a message when a diff shows files match? fpp666 Programming 3 07-16-2009 08:19 AM
'find' gives different results when using -mtime with -print vs -exec BrianK Linux - General 1 12-08-2008 10:32 AM
Sed/Awk: print lines between n'th and (n+1)'th match of "foo" xaverius Programming 17 08-20-2007 11:39 AM
compare two files in C and print the line when they do not match calorie712 Programming 7 04-13-2006 01:56 AM
Results of print job a nightmare loadedmind Linux - Hardware 4 01-04-2004 10:09 PM


All times are GMT -5. The time now is 03:35 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration