LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-13-2013, 12:51 AM   #1
adi05
LQ Newbie
 
Registered: Mar 2013
Posts: 2

Rep: Reputation: Disabled
matching the data from two text files and printing it in the form of a matrix in the


based on column 1 & 2 of file1 and Column1 and row1 of file 2 i want to extract the value of column 3 of file1 and insert write the matrix as shown below in desired output. Files are unsorted. I am a beginner to programming.Any help in perl would be greatly appreciated. Thanks

eg1.txt
sn1 Jack CC
sn3 Jack TT
sn2 Jack TT
sn4 Jack CC
sn4 Mac CC
sn1 Mac AA
sn3 Mac TT
sn2 Mac AA

eg2.txt
Jack Mac
sn1
sn2
sn3
sn4


DESIRED OUTPUT

Jack Mac

sn1 CC AA

sn2 TT AA

sn3 TT TT

sn4 CC CC

below is my code, i'm certainly doing alot wrong here. Plz help

use warnings;
use strict;
use IO::Handle;
use FileHandle;
my %account;
$file1 = "C:\perl\bin\eg1.txt";
$file2= "C:\perl\bin\eg2.txt";
$outfile = "C:\perl\bin\out3.txt";
open( my $fh1,"<", "eg1.txt" ) or die "$!";
while( my $line = <$fh1> ) {
my @values = split ' ', $line;
$account{$values[0]} = $values[1];

print"$values[2]\n";
}
close $fh1;
open( my $out_fh, ">", "out.txt" ) or die "$!";
open( my $fh2, "<", "eg2.txt" ) or die "$!";
while( my $line = <$fh2> ) {
my @values = split ' ', $line;
print $out_fh join $values[0], $account{$values[0]}, $values[1];
#print"$out_fh\n";
}
close $out_fh;
close $fh2;
 
Old 03-13-2013, 03:26 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,356

Rep: Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367
Re desired output
Code:
Jack Mac

sn1 CC AA

sn2 TT AA

sn3 TT TT

sn4 CC CC
Ignoring blank lines, I get the matching I think, except the first line; I don't see why that occurs.
Can you clarify?
 
Old 03-13-2013, 06:01 AM   #3
adi05
LQ Newbie
 
Registered: Mar 2013
Posts: 2

Original Poster
Rep: Reputation: Disabled
chris..i didnt understand ..is the code i gave working?
 
  


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
matching and printing fields of two files ksaad Programming 3 11-07-2011 02:15 PM
[SOLVED] No form feed after printing short form gallicbear Linux - Hardware 3 08-17-2010 02:44 AM
Delete files matching text - using a loop anjanesh Linux - General 3 09-26-2009 03:19 AM
Delete files containing matching text hessodreamy Linux - Newbie 11 05-21-2008 09:05 PM
data type for text files in a database creolophus Programming 1 06-20-2007 11:17 PM


All times are GMT -5. The time now is 07:25 AM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration