0
votes

PFB the scenario. I have two files

file1

firstName1 LastName1
FirstName2 LastName2

File2

FirstName1 LastName1
FirstName2 LastName2

Now I want to compare FirstName1 of file1 with that of file2. If it matches then compare LastName1 of file1 with that of File2.

If any of these values doesn't mach then the record should be written to log file.

Once we have done this, move to second record.

Can some one put some insight into it......

2
First: Welcome to SO. This might help you getting along with SO: stackoverflow.com/help/how-to-ask To answer you question diff is the tool you need - Fabian H.

2 Answers

0
votes

Use following standard unix commands

diff
sdiff

This page will also help to understand difference between diff and sdiff.

You can also write a script to compare records from the 2 files.

0
votes

diff is the best for what you want to do, but here is a snippet if you want more actions in that kind of situation, assuming the files are properly formatted

./compare.sh <file1> <file2>

compare.sh:

#!/bin/bash

line_number=0

cat $1 | while read line_f1; do

    line_number=$((line_number + 1))

    line_f2=$(cat $2 | sed "${line_number}q;d")

    echo "line f1 : ${line_f1}"
    echo "line f2 : ${line_f2}"

    firstname_f1=$(echo ${line_f1} | cut -f1 -d' ')
    firstname_f2=$(echo ${line_f2} | cut -f1 -d' ')
    lastname_f1=$(echo ${line_f1} | cut -f2 -d' ')
    lastname_f2=$(echo ${line_f2} | cut -f2 -d' ')

    echo "firstname f1 : ${firstname_f1}"
    echo "firstname f2 : ${firstname_f2}"
    echo "lastname f1 : ${lastname_f1}"
    echo "lastname f2 : ${lastname_f2}"

    if [ ! "${firstname_f1}" = "${firstname_f2}" ]; then
        echo "Differents Firstnames"
    fi

    #... place here other tests ...

done