LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-01-2010, 10:40 AM   #1
vaibhavs17
Member
 
Registered: Mar 2009
Posts: 49

Rep: Reputation: 1
Angry 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
 
Old 03-01-2010, 10:52 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Have you looked at the "cmp" and "diff" commands?
 
Old 03-01-2010, 11:01 AM   #3
vaibhavs17
Member
 
Registered: Mar 2009
Posts: 49

Original Poster
Rep: Reputation: 1
Angry

I want it characterwise difference not line by line diff.

Please help
 
Old 03-01-2010, 11:18 AM   #4
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
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.
 
Old 03-01-2010, 01:05 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
You can also do some processing using the output of diff.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash scripting: parsing a text file character-by-character Completely Clueless Programming 13 08-12-2009 09:07 AM
Need a script to search and replace text in file using shell script unixlearner Programming 14 06-21-2007 10:37 PM
How to find and change a specific text in a text file by using shell script Bassam Programming 1 07-18-2005 07:15 PM
Search and replace text in file using shell script? matthurne Linux - Software 2 11-02-2004 10:11 AM
how to convert a text file into shell script meng_en Linux - General 9 10-15-2002 10:46 PM


All times are GMT -5. The time now is 09:46 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration