Solved

Compare first 3 fields between two files

Posted on 2011-02-10
3
369 Views
Last Modified: 2012-05-11
I need to compare just the first three fields between 2 different files.

the first file have lines with 3 fields
servername1 WINDOWS daily

The second file have lines with 5 fields
servername1 WINDOWS daily 24 remotehost
servername2 WINDOWS daily 24 remotehost

I want to print to a third file where the fiirst 3 field exist in the seonds file but do not exist in the first file, so in this instance the third file would contain the second line from the second file

servername2 WINDOWS daily 24 remotehost




0
Comment
Question by:MatthewF
  • 2
3 Comments
 
LVL 26

Accepted Solution

by:
wilcoxon earned 450 total points
Comment Utility
This should do what you want...
#!/usr/local/bin/perl

use strict;
use warnings;

# get the two filenames from the command line
my $fil1 = shift || die "Usage: $0 file1 file2 output_file";
my $fil2 = shift || die "Usage: $0 file1 file2 output_file";
my $ofil = shift || die "Usage: $0 file1 file2 output_file";

my %seen;
open IN, $fil1 or die "could not open $fil1: $!";
while (<IN>) {
    chomp;
    my @arr = split /\s+/; # split the line on whitespace
    $seen{$arr[0]}{$arr[1]}{$arr[2]}++;
}
close IN;

open OUT, ">$ofil" or die "could not write $ofil: $!";

open IN, $fil2 or die "could not open $fil2: $!";
while (<IN>) {
    chomp;
    my @arr = split /\s+/; # split on whitespace again
    # write the line to the output file unless we saw it in file1
    print OUT $_, "\n" unless exists($seen{$arr[0]}{$arr[1]}{$arr[2]});
}
close IN;
close OUT;

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
perl -ane 'print unless $s{"@F[0..2]"}++ or @ARGV' first second > third
0
 
LVL 26

Expert Comment

by:wilcoxon
Comment Utility
As always, ozo is king of the one-liners.

I'm an expert at perl but often learn something new (or at least make me think) from ozo's one-liners.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now