INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

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

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

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

(OP)
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.


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

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

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


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

(OP)
Thanks John, earlier i used the same command, but it is showing line number not exact position.

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

The command "vimdiff" will highlight the different characters in a line.

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

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

Also look at "cmp".

CODE

# cat f1
one
two
three
four
five
# cat f2
one
two
thRee
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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close