![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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| |
|
||||
|
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 |
|
||||
|
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: /' |
|
||||
|
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' |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|