I am new in this forum, I have two files which needs to compare, the files don't have delimiter and data are present in position-wise so would require compare line by line with each character. I used below sdiff command but it is not showing to me expected output, it shows only line number which are different but not position of character.

sdiff file1 file2 | grep "|" > log file

I tried below code , Can you please help on same.

for i in `cat  /home/okanung/test_handoff/testfile1`
for j in `cat  /home/okanung/test_handoff/testfile2`

for ((k=0;k <=`echo $i | wc -c `;k++))
if [ "$a" != "$b" ]
lineno=`grep -n "$i" /home/okanung/test_handoff/testfile1 | cut -d ":" -f 1`
echo "mismatch is at line no $lineno  at position $k" | sort -n | uniq -c >> /home/okanung/test_handoff/test_log_file

File format : 06001234567800000009 20160226100TD22000002 04.00000.0000000000000030.000000000000028.800000 0000000000001.200000 0000000000000.4200000000000000000.4000000000000000000.020000‌​0000000000000.460000‌​0000000000000.440000‌​0000000000000.020000‌​0000000000000.000000‌​Standard 0 221-PASS-RATE 201603075 10UW0000054321AB00000361593804INR20160226EUR0010000010000000‌​00000030.00000000000‌​000000.0004.00004.00‌​000000000000

  • It would be tremendously easier if you showed samples of the files you want to compare. – maulinglawns Jan 25 '17 at 12:53
  • Hi and welcome to the site. A few points: 1) We can't really help you unless you give us an example of your input and show us an example of your expected output. 2) Are you sure you need this? There are already many tools that can do it for you. Do you really need to script your own solution or would those tools be OK? 3) If you do need to script something, does it have to be in bash? The shell is an awful tool for this sort of thing. – terdon Jan 25 '17 at 12:57
  • try to use diff command. diff text1 text2. There are many options that you can use. – Slh47 Jan 25 '17 at 13:02
  • I am having transaction file, this is for testing so everyday have to generate a file and compare same with yesterday's file to check the functionality and calculations.I used below command also : sdiff file1 file2 | grep "|" > logfile – user212332 Jan 25 '17 at 13:03
  • 1
    Please edit your question to add extra information. It is hard to read and easy to miss in the comments. Also, comments can be deleted with no warning. And show us examples of your input and desired output. Your comment talks about comparing "words" but your script compares "characters". If sdiff didn't work for you, tell us why. Explain what you need that standard tools like cmp or diff don't provide. – terdon Jan 25 '17 at 13:20

Use cmp:

$ cat file1.txt
This file
is the same
as the other

$ cat file2.txt
This file
is almost the same
as the other

$ cmp file1.txt file2.txt
file1.txt file2.txt differ: char 14, line 2

char 14 refers to the first character in the file that differs.

For a visual representation of the word difference, use wdiff:

$ wdiff file1.txt file2.txt
This file
is {+almost+} the same
as the other
  • Thanks for your reply but wdiff is not working at Linux, i tried already, also used this one also :--> sdiff file1 file2 | grep "|" > logfile – user212332 Jan 25 '17 at 13:07
  • @user212332 wdiff is most definitely available on Linux (check your package-manager). That sdiff+grep pipeline is identical to sdiff -s, BTW. – Kusalananda Jan 25 '17 at 13:10
  • Yes but it is not fulfilling my requirement,i want to compare each words of first line with first line of second file and it should shows the differences like at which line and position differences are there. – user212332 Jan 25 '17 at 13:12
  • @user212332 indication char 14 in this really nice solution is not enough? Difference is at char 14, line2. – George Vasiliou Jan 25 '17 at 17:27

You can use the diff command to see which line are different in each file.

diff file1.txt file2.txt

and this will give you the line that differs, in this case :

file1.txt :

"same line,

same line,

different line,

same line."

file2.txt :

"same line,

same line,

same line,

same line."

$> diff file1.txt file2.txt

< line 3 : different line,
> line 3 :  same line,
  • Thanks, But this is will show only at which line having differences, but not showing difference at position wise. – user212332 Jan 25 '17 at 16:44
  • It shows the entire line, so you can find the difference by yourself by searching a little bit. – user212219 Jan 25 '17 at 18:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.