Solved

Compare first 3 fields between two files

Posted on 2011-02-10
3
375 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Six Sigma Control Plans

724 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