Solved

Compare first 3 fields between two files

Posted on 2011-02-10
3
374 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Suggested Solutions

Title # Comments Views Activity
how to extract data from a table into a fixed length file 10 63
perl to convert excel to csv 3 338
Edit Odbc.ini using perl module 2 96
what are these modules in perl script 4 107
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…

738 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