Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to compare two files character wise in Unix shell scripting?

Status
Not open for further replies.

omkanungo

Technical User
Jan 26, 2017
2
US
Hello Everyone,

My question is How can we compare two files might be having same data with few differences, Data are not in columns, but they are segregated position wise.
So we would require to compare each line characters with another.
I tried to use sdiff command also but it shows at which line differences are present.

sdiff $file1 $file2 | cat -n |grep "|" > log file

Also tried to create one more script but it is taking so much time.
================================================================================
for i in `cat /testfile1`
do
for j in `cat /testfile2`
do

for ((k=0;k <=`echo $i | wc -c `;k++))
do
a=${i:k:1}
b=${j:k:1}
if [ "$a" != "$b" ]
then
lineno=`grep -n "$i" /testfile1 | cut -d ":" -f 1`
echo "mismatch is at line no $lineno at position $k" >> /test_log_file
fi
done

#cat /home/okanung/test_handoff/test_log_file| sort -n |uniq -c >final_test_log_file

done
done
================================================================================

Please help for same.If anybody have any idea on same.You can share your own script as well.


 
you can try diff rather than sdiff (side by side diff)

==================================
advanced cognitive capabilities and other marketing buzzwords explained with sarcastic simplicity


 
Thanks John, earlier i used the same command, but it is showing line number not exact position.
 
The command "[tt]vimdiff[/tt]" will highlight the different characters in a line.

The tool "Beyond Compare" will also find specific differences within the line.

 
Also look at "[tt]cmp[/tt]".

Code:
# cat f1
one
two
th[highlight #FCE94F]r[/highlight]ee
four
five
# cat f2
one
two
th[highlight #FCE94F]R[/highlight]ee
four
five
# cmp f1 f2
f1 f2 differ: byte 11, line 3
# cmp -b f1 f2
f1 f2 differ: byte 11, line 3 is 162 r 122 R
#

Note that the byte number is the offset into the file, not the particular row the difference is on.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top