LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   shell script comparison for two file(text) character wise (http://www.linuxquestions.org/questions/programming-9/shell-script-comparison-for-two-file-text-character-wise-792360/)

vaibhavs17 03-01-2010 10:40 AM

shell script comparison for two file(text) character wise
 
Hi All,

This forum helped me a lot! Thanks for your help.

Well, Again I am in problem I need to write a shell script which can compare two files(text files) character wise.

eg.
underscore is space.
------FILE 1---------------
A_B_C
D_E_F
G_H_I
------------------------------

--------FILE 2--------------
A_BC
D_E_F
GH__I
---------------------------

Output should be
_C -> C_
_H -> H_

Actual problem: I need to write a shell script which can give me difference character by character not by line (using comm)

Please HELP HELP!

Thanks,
Vaibhav

pixellany 03-01-2010 10:52 AM

Have you looked at the "cmp" and "diff" commands?

vaibhavs17 03-01-2010 11:01 AM

I want it characterwise difference not line by line diff.

Please help

H_TeXMeX_H 03-01-2010 11:18 AM

Well, as pixellany said, cmp may do it:

Code:

bash-3.1$ printf "A B C\nD E F\nG H I\n" > 1
bash-3.1$ cat 1
A B C
D E F
G H I
bash-3.1$ printf "A BC \nD E F\nGH  I\n" > 2
bash-3.1$ cat 2
A BC
D E F
GH  I
bash-3.1$ cmp -l 1 2
 4  40 103
 5 103  40
14  40 110
15 110  40
bash-3.1$ cmp -bl 1 2
 4  40      103 C
 5 103 C    40 
14  40      110 H
15 110 H    40

Now, this may not be exactly what you want, but it tells you exactly what is different, and you can work with it to get what you want.

pixellany 03-01-2010 01:05 PM

You can also do some processing using the output of diff.


All times are GMT -5. The time now is 12:35 PM.