LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 10-27-2009, 04:12 AM   #1
kariagekun
LQ Newbie
 
Registered: Oct 2009
Posts: 11

Rep: Reputation: 0
compare two files in perl


Dear guys,

I want to compare two files in perl, I have two files file1.txt & file2.txt. if column1 on file2.txt match column1 on file1.txt then I want my result on file3.txt (column1 column2 file1.txt + column1 column2 column3 file2.txt). this problem was solved with "awk" but I want to do in perl.

file1.txt
---------
column1 column2
------- -------
linux-2049 1
linux-2817 1
linux-3073 1
linux-3329 1
bsd-3585 1
bsd-3841 1
bsd-33 1
bsd-6177 2
bsd-10209 1
linux-2 2
linux-2050 2
linux-2818 2
linux-3074 2

file2.txt
---------
column1 column2 column3
------- ------- --------
bsd-33 TBTJX more-256
bsd-6177 TBTJX more-257


file3.txt
----------

bsd-33 1 bsd-33 TBTJX more-256
bsd-6177 2 bsd-6177 TBTJX more-257

Many thanks for your help.

Last edited by kariagekun; 10-27-2009 at 04:19 AM.
 
Old 10-27-2009, 04:18 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,696
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
if Perl is not a must
Code:
#!/bin/bash
awk 'FNR==NR{ a[$1]=$2; next}
( $1 in a){
   o=$1
   $1=""
   print o,a[o]$0
}' file1 file2
 
Old 10-27-2009, 04:22 AM   #3
kariagekun
LQ Newbie
 
Registered: Oct 2009
Posts: 11

Original Poster
Rep: Reputation: 0
Thank you for your script, but it was done with awk. could you help me how to write in perl?.
 
Old 10-27-2009, 04:43 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,696
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by kariagekun View Post
could you help me how to write in perl?.
ok i will help you. first you need to learn how to open and read files. for that, read perldoc -f open. Then you need to learn at least how to use hashes. for that, turn to perldoc perldsc. that's all for a start. please do the rest
 
Old 10-27-2009, 04:59 AM   #5
kariagekun
LQ Newbie
 
Registered: Oct 2009
Posts: 11

Original Poster
Rep: Reputation: 0
#!/usr/bin/perl -w

use strict;

my $data = "file1.txt";
my $data1 = "file2.txt";

my @idd;

open (FILE, "<$data") or die $!;
while (<FILE>){
chomp;
push (@idd, (split (/ /,$_)));
}
close FILE;

open (FILE, "<$data1") or die $!;
my @array;
foreach my $line(<FILE>){
chomp $line;
my @fields = split/ /,$line;
if (grep{$fields[0] == $_}@idd){
$line = join (" ",@fields);
}
push (@array,$line);

I have made on perl script but still not give me desired output.
 
Old 10-27-2009, 07:38 PM   #6
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,289

Rep: Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034
As ghostdog74 said, this is a classic 'use a hash' problem; see the tutorials here http://www.perlmonks.org/?node=Tutorials
 
  


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
Where exactly Perl & C/C++ are getting different and unique compare to each other..? deepak_p86 Programming 4 07-14-2009 11:52 PM
Perl: Want to compare CGI input with data from a MySQL table resetreset Programming 1 11-20-2008 06:58 AM
How to compare two lists (arrays) in perl WindowBreaker Programming 13 04-24-2008 03:01 AM
Perl List::Compare printing PB0711 Programming 2 08-10-2006 12:59 PM
perl files download instead of parsed on apache2 mod-perl2 not parsing perl files zeigerpuppy Debian 1 02-16-2006 05:31 AM


All times are GMT -5. The time now is 10:41 PM.

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