How to compare two text files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to compare two text files
# 1  
Old 11-20-2011
How to compare two text files

Hi Team,

Could you please help me on below one ..


etrademail1.txt


etDefaultLogin=pdayanan
mail=poojaaragam.dayanand@exchange.etr.comx
employeeNumber=31567



etDefaultLogin=sudrupa
mail=sudrupa.ayanand@exchange.etr.comx
employeeNumber=318967


etDefaultLogin=gurathi
mail=gurothi.anand@exchange.etr.comx
employeeNumber=3189678

maillist2.txt


eruid=dwolinsk
eradesmtpemail=Xdaniel.wolinski@exchange.etr.com


eruid=pdayanan
eradesmtpemail=Xpoojaaragam.dayanand@exchange.etr.comx


eruid=sbtura
eradesmtpemail=Xsbtura.wolinski@exchange.etr.com


eruid=durapda
eradesmtpemail=Xdurapas.upreras@exchange.etr.com

Expected output in separate file output.txt ------> mapping with etDefaultLogin=pdayanan and eruid=pdayanan

etDefaultLogin=pdayanan
eradesmtpemail=Xpoojaaragam.dayanand@exchange.etr.comx
employeeNumber=31567

etDefaultLogin=sudrupa
eradesmtpemail=Xsudrupa.ayanand@exchange.etr.comx
employeeNumber=318967

etDefaultLogin=gurathi
eradesmtpemail=Xgurothi.anand@exchange.etr.comx
employeeNumber=3189678


Thanks,
Srinivasarao K
# 2  
Old 12-01-2011
Invoke this following perl script as:
Code:
maillistjoiner maillist etradedatafile

Script is:
Code:
#! /usr/bin/perl

use strict;
use warnings;
use File::Basename;

$/ = "\n\n";
$\ = "\n";

my %MAILLIST;
my @FIELDLIST = qw{ etDefaultLogin eradesmtpemail employeeNumber };

# data parse

sub parse($) {
    my $X = {};

    foreach my $entry (split /\n/, $_[0]) {
        my ($key, $value) = $entry =~ m{^\s*([^=]+)=(\S+)};
        next unless defined $key;

        $X->{$key} = $value;
    };

    return $X;
}

# Read maillist

my $list = shift(@ARGV);
die unless defined $list;

open FH, '<', $list;

while (<FH>) {
    my $X = parse($_);

    unless (defined $X->{eruid}) {
    #   print STDERR $list, '(', $., '): missing eruid';
        next;
    }

    unless (defined $X->{eradesmtpemail}) {
        print STDERR $list, '(', $., '): missing eradesmtpemail for ', $X->{eruid};
        next;
    }

    $MAILLIST{$X->{eruid}} = $X->{eradesmtpemail};
}

close FH;

# Read datafile

my $data = shift(@ARGV);
die unless defined $data;

open FH, '<', $data;

while (<FH>) {
    my $X = parse($_);

    unless (defined $X->{etDefaultLogin}) {
    #   print STDERR $data, '(', $., '): missing etDefaultLogin';
        next;
    }

    if (defined $MAILLIST{$X->{etDefaultLogin}}) {
        $X->{eradesmtpemail} = $MAILLIST{$X->{etDefaultLogin}};
    }
    else {
        $X->{eradesmtpemail} = $X->{mail};
    }

    foreach my $key (@FIELDLIST) {
        if (defined $X->{$key}) {
            print $key, '=', $X->{$key};
        }
    }

    print $\;
}

close FH;

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare two fields in text files?

Hi, I have two text files, compare column one in both the files and if it matches then the output should contain the id in column one, the number and the description. Both the files are sorted. Is there a one liner to get this done, kindly help. Thank you File 1: NC_000964 92.33 ... (2 Replies)
Discussion started by: pulikoti
2 Replies

2. Shell Programming and Scripting

Script to compare two text files

i am working on a shell script and need help in the comparing part of it. for e.g. there two text files like this: file1.txt name1 name2 name3 file1 has to be comared with file2 defaultfile.txt name1 name2 name3 name4 and during comparision with defaultfile.txt if... (2 Replies)
Discussion started by: draghun9
2 Replies

3. Shell Programming and Scripting

Compare two text files and output difference

Hi experts, I am trying to compare two text files and output the difference to another file. I'm not strictly looking for differences in text but additional text at the end of one file that isn't in another, so basically comparing the file 2 against file 1 and printing any additional text to... (9 Replies)
Discussion started by: martin0852
9 Replies

4. UNIX for Dummies Questions & Answers

Compare two text files

Hello guys, I have file1 and file2, two text files containing various lines. I'm trying to find a way to compare file1 and file2: If the first 7 characters of a line in file2 match the first 7 characters of a line in file1, then do not do anything. Print out the lines of file1 (in file3,... (3 Replies)
Discussion started by: bobylapointe
3 Replies

5. Shell Programming and Scripting

To compare the content of two text files

I have two files, sec.txt(1st File) 3172 disp.txt(2nd file) the file name is *********** 45676 Now i want to compare the value in sec.txt file with disp.txt file Excatly i want to compare the value 3172 in first file and 45676 in second file. i want to compare the first line of... (11 Replies)
Discussion started by: rammm
11 Replies

6. Shell Programming and Scripting

Compare two text files and print matches

Hi, I am looking for a way to compare two text files and print the matches. For example; File1.txt 89473036 78474384 48948408 95754748 47849030 File2.txt 47849030 46730356 16734947 78474384 36340047 Output: (11 Replies)
Discussion started by: lewk
11 Replies

7. Shell Programming and Scripting

Compare 2 files and output only the different text.

I know the diff does this but it does output more info than just the different text (e.g. $ diff file1 file2 29a30 > /home/alex/Pictures/hello.jpg 1694a1696 > /home/alex/Pictures/hi.jpg ) How can I make it output only /home/alex/Pictures/hello.jpg /home/alex/Pictures/hi.jpg ? thank... (2 Replies)
Discussion started by: hakermania
2 Replies

8. Shell Programming and Scripting

Compare two text files and Only show the differences

Hi experts, I'mvery new to shell scripting and learning it now currently i am having a problem which may look easy to u :) i have two files File 1: Start :Thu Nov 19 10:33:09 2009 ABCDGFSDJ.txt APDemoNew.ppt APDemoOutline.doc ARDemoNew.ppt ARDemoOutline.doc File 2: Start... (10 Replies)
Discussion started by: CelvinSaran
10 Replies

9. UNIX for Advanced & Expert Users

How to compare two text files in column wise?

Hi All, I have two txt files like this File1: no name ---------- 12 aaaa 23 bbbb 55 cccc File2 dname dno ------------ civil 33 mech 55 arch 66 Now i want to compare col1 from File and col2 from File2, if its match i want fetch all columns from... (3 Replies)
Discussion started by: psiva_arul
3 Replies

10. UNIX for Dummies Questions & Answers

compare text files

This may be the 3rd time I'm posting this question. I'm so new here that I'm not even sure how to post! I'm trying to compare two files but can't do a line by line comparison so comm and diff are out. I've been told that I would need to use the awk programing language. I've looked up what I... (14 Replies)
Discussion started by: jimmyflip
14 Replies
Login or Register to Ask a Question