The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
compare XML/flat file with UNIX file system structure shafi2all High Level Programming 6 08-15-2008 03:15 AM
Check File Exists and compare to previous day file script rbknisely Shell Programming and Scripting 3 02-07-2008 11:53 AM
Need Script to check file exist and compare rbknisely UNIX for Dummies Questions & Answers 1 01-16-2008 01:08 AM
compare file size from a output file from a script moustik Shell Programming and Scripting 7 11-07-2007 10:17 AM
Help- Unix File Compare- Struggling guiguy UNIX for Advanced & Expert Users 2 01-06-2007 08:32 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-27-2008
autosys_nm autosys_nm is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 8
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 (permalink)  
Old 08-27-2008
manosubsulo manosubsulo is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 87

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 (permalink)  
Old 08-27-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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 (permalink)  
Old 08-27-2008
manosubsulo manosubsulo is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 87
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 (permalink)  
Old 08-27-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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'

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:28 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0