Solved

Compare first 3 fields between two files

Posted on 2011-02-10
3
370 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
ID: 34866132
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
ID: 34866249
perl -ane 'print unless $s{"@F[0..2]"}++ or @ARGV' first second > third
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 34866276
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Perl Script to append xml file 32 191
Question about @INC variable in perl 1 55
Regular Expression question to filter with negation. 6 89
combine multiple lines 2 42
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

911 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

15 Experts available now in Live!

Get 1:1 Help Now