LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 07-13-2010, 06:23 PM   #1
0bfuscated
LQ Newbie
 
Registered: Jan 2010
Distribution: Debian / RH-CentOS / Fedora
Posts: 6

Rep: Reputation: 0
Perl Script needed to be reversed to output matching, not non-matching


I am having a rough time attempting to reverse this tiny script to outfile matching fields. It only outfiles the non-matching. I tried playing with the regular expressions and of course it did not work. Could someone give me some insight as to how to make this reverse its role and outfile matching fields?

Thanks!

Code:
#!/bin/perl
use strict;
use warnings;

my $f1 = 'file2';
my $f2 = 'hosts';
my $outfile = 'hosts1';
my %results = ();

open FILE1, "$f1" or die;
while(my $line = <FILE1>){
   $results{$line}=1;
}
close(FILE1);

open FILE2, "$f2" or die;
while(my $line =<FILE2>) {
   $results{$line}++;
}
close(FILE2);


open (OUTFILE, ">$outfile") or die;
foreach my $line (keys %results) {
   print OUTFILE $line if $results{$line} == 1;
}
close OUTFILE;
 
Old 07-13-2010, 06:51 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by 0bfuscated View Post
I am having a rough time attempting to reverse this tiny script to outfile matching fields. It only outfiles the non-matching. I tried playing with the regular expressions and of course it did not work. Could someone give me some insight as to how to make this reverse its role and outfile matching fields?

Thanks!

Code:
#!/bin/perl
use strict;
use warnings;

my $f1 = 'file2';
my $f2 = 'hosts';
my $outfile = 'hosts1';
my %results = ();

open FILE1, "$f1" or die;
while(my $line = <FILE1>){
   $results{$line}=1;
}
close(FILE1);

open FILE2, "$f2" or die;
while(my $line =<FILE2>) {
   $results{$line}++;
}
close(FILE2);


open (OUTFILE, ">$outfile") or die;
foreach my $line (keys %results) {
   print OUTFILE $line if $results{$line} == 1;
}
close OUTFILE;
Well, your post appears obfuscated to me. I.e. I can't understand what you want to achieve. I think you should more clearly explain what are the inputs, what is the desired output and what is the actual output.

To explain also means showing examples trough print statements and/or test cases.
 
Old 07-20-2010, 10:51 AM   #3
Lost_Oracle
LQ Newbie
 
Registered: Jul 2010
Distribution: Red Hat Commercial
Posts: 16

Rep: Reputation: 2
0bfuscated,

It looks like you might be trying to print out all of the matching lines from two compared text files. Is that the case?

If so, there is likely a better way to do it. Most langauges have some sort of string compare function that returns a 0 if the two strings are identical. I'm sure perl has something similar. That might save you some hassle. A few if statements might do the trick in printing those that match rather than those that don't. Try setting your $results{$line} = 0 if they don't match. It'll make the if statements much easier to deal with.

If you want to match all matching lines between the two files, and not just those that are in identical spots in the files, you could loop through the file.
Note: this will take much more processing power and time, especially for very large files.

A sample of your input and what you want to get for output would be very helpful.


Lost
 
  


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
[SOLVED] perl matching?? ashok.g Programming 2 02-03-2010 12:03 AM
pattern matching help in perl kdelover Programming 7 11-07-2009 07:01 PM
LXer: Streamlined Perl Number Matching Script For Unix Or Linux LXer Syndicated Linux News 0 09-18-2008 12:00 PM
perl matching script saltydog4791 Programming 4 12-05-2007 04:52 PM
Perl pattern matching in VB rigel_kent Programming 1 05-30-2006 11:00 AM


All times are GMT -5. The time now is 07:52 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