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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
07-13-2010, 07:23 PM
|
#1
|
LQ Newbie
Registered: Jan 2010
Distribution: Debian / RH-CentOS / Fedora
Posts: 6
Rep:
|
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;
|
|
|
07-13-2010, 07:51 PM
|
#2
|
Senior Member
Registered: May 2005
Posts: 4,481
|
Quote:
Originally Posted by 0bfuscated
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.
|
|
|
07-20-2010, 11:51 AM
|
#3
|
LQ Newbie
Registered: Jul 2010
Distribution: Red Hat Commercial
Posts: 16
Rep:
|
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
|
|
|
All times are GMT -5. The time now is 01:13 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|