Unix script to compare the two file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unix script to compare the two file
# 1  
Old 08-27-2008
Unix script to compare the two file

Hi,
I want to compare two | delimited files.Awk is not working in my unix box.So plz give alternate solutions.

Please see the below code:

file1=$1
file2=$2

num_of_records_file1=`awk ' END { print NR } ' $file1`
num_of_records_file2=`awk ' END { print NR } ' $file2`

i=1
while [ "$i" -le "$num_of_records_file1" ]
do
sed -n "$i"p $file1 > file1_temp
sed -n "$i"p $file2 > file2_temp

diff file1_temp file2_temp>file_temp

echo "Comparing $i line of fil1 with file2:"
awk NR==2 file_temp|sed 's/</Old:/g'
awk NR==4 file_temp|sed 's/>/New:/g'
i=`expr $i + 1`
done

I have 2 files File1.txt and File2.txt

File1.txt
123|Rosy|Chicago|
234|stella|michigan|
999|Richard|NJ|

File2.txt
123|Rosy|Chicago|
235|Stella|michigan|
999|Richard|NJ|ABN

I want to compare the two files and get the output as below:
Old:234|stella|michigan
New:235|Stella|michigan|
# 2  
Old 08-27-2008
Code:
cnt=1
for line in `comm -3 a.txt b.txt | paste - -`
do
        if [ "$cnt" -eq "2" ]
        then
                echo "Old:"$prev
                echo "New:"$line
                cnt=1
        else
                cnt=`expr $cnt + 1`
                prev="$line"
        fi
done


Last edited by Franklin52; 08-27-2008 at 04:33 AM.. Reason: add code tags
# 3  
Old 08-27-2008
Doesn't the lack of an ABN at the end of the last file count as a difference?

I can't get paste - - to do anything useful here, and the approach by manosubsulo seems a bit inefficient. Besides, comm requires sorted input, which does not seem to apply here.

What's wrong with just running diff on the two files?

Code:
diff a.txt b.txt | sed -e 's/^< /Old:/' -e 's/^> /New: /'

# 4  
Old 08-27-2008
Era,

as per suggestion, sample output will be as,

2,3c2,3
Old:234|stella|michigan|
Old:999|Richard|NJ|
---
New: 235|Stella|michigan|
New: 999|Richard|NJ|ABN

But autosys_nm needs the output in different format.
# 5  
Old 08-27-2008
Well, the removal of the diff annotations isn't hard to add to the sed script, and whether or not the Old: and New: lines should be adjacent or not isn't clearly specified. If they are required to be adjacent, perhaps sort the output from diff on the first field?

Code:
diff a.txt b.txt | sort -d -t '|' -n | sed -n -e 's/^< /Old: /p' -e 's/^> /New: /p'

Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script (sh file) logic to compare contents of one file with another file and output to file

Shell script logic Hi I have 2 input files like with file 1 content as (file1) "BRGTEST-242" a.txt "BRGTEST-240" a.txt "BRGTEST-219" e.txt File 2 contents as fle(2) "BRGTEST-244" a.txt "BRGTEST-244" b.txt "BRGTEST-231" c.txt "BRGTEST-231" d.txt "BRGTEST-221" e.txt I want to get... (22 Replies)
Discussion started by: pottic
22 Replies

2. Shell Programming and Scripting

File compare in UNIX

I have two files which is having components and its version inside that, cat file1 com.acc.invm:FNS_PROD com.acc.invm:FNS_TEST_DCCC_Mangment com.acc.invm:FNS_APIPlat_BDMap com.acc.invm:SendEmail com.acc.invm:SendSms cat file2 com.acc.invm:FNS_PROD 94.0.5... (11 Replies)
Discussion started by: rakeshtomar82
11 Replies

3. UNIX for Dummies Questions & Answers

Compare two file in UNIX

Hi, how to compare all the differences from two files in unix ? plz provide the screenshots. Thanks, Dasaradha (1 Reply)
Discussion started by: dasaradha
1 Replies

4. UNIX for Dummies Questions & Answers

I want to compare to alphanumeric value in a unix shell script.

#!/bin/sh b= SERVER if ; then echo "hostname $a is same" ____________________________ (17 Replies)
Discussion started by: Nsharma3006
17 Replies

5. Homework & Coursework Questions

Compare to values in a file in unix

Here is sample file ===============Index 0=================== isActive=0, Input=1, Output=1, Status=1 State = Future , PRIMARY UnderCount=2 inCount=2 outCount=0 SCount=673 -- ===============Index 1=================== isActive=0, Input=1, Output=1, Status=1 ... (1 Reply)
Discussion started by: sooda
1 Replies

6. UNIX for Dummies Questions & Answers

Unix Script to compare two files

Hello, I have a dat file nctilllist.dat which will be present in the directory path "/usr/lpp/web-data/mfg/nct/file-data/nctilllist.dat" nctillist.dat will have reference to files like DP100001.jpg,DP10002.PDF,DP100003.doc on the path /usr/lpp/web-data/mfg/nct/file-data will have... (12 Replies)
Discussion started by: gayathrivm
12 Replies

7. Programming

compare XML/flat file with UNIX file system structure

Before i start doing something, I wanted to know whether the approach to compare XML file with UNIX file system structure. I have a pre-configured file(contains a list of paths to executables) and i need to check against the UNIX directory structure. what are the various approches should i use ? I... (6 Replies)
Discussion started by: shafi2all
6 Replies

8. UNIX for Advanced & Expert Users

Help- Unix File Compare- Struggling

I had posted this earlier about 3 weeks ago and had recieved a response and I did sort both the files and the comm command is still not working. Can someone please assist me, I would really appreciate it. Below is what I am trying to do I need to compare File A with File B and create FILE C... (2 Replies)
Discussion started by: guiguy
2 Replies
Login or Register to Ask a Question