shell script comparison for two file(text) character wise

This is a discussion on shell script comparison for two file(text) character wise within the Linux Programming forums, part of the Platform Specific Boards category; Hi All, This forum helped me a lot! Thanks for your help. Well, Again I am in problem I need ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    40

    Question 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

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,556
    diff?
    cmp?

    Both will more or less give you the information.

    But you might have to post-process the results with something else to get the desired format.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by vaibhavs17 View Post
    Actual problem: I need to write a shell script which can give me difference character by character not by line (using comm)
    Personally, I would rather eat glass than use bash for this but here is a starting point:
    Code:
    #!/bin/bash
    
    i=0
    while read line; do
    	file[$i]=$line;
    	i=$((i+1));
    done < "test.sh"
    
    i=0
    while [ $i -lt ${#file[*]} ]; do
    	j=0
    	while [ $j -lt ${#file[$i]} ]; do
    		echo ${file[i]:$j:1};
    		j=$((j+1));
    	done;
    	i=$((i+1));
    done;
    Doesn't that look like fun? This stores "test.sh" in an array of lines, then prints each line one character at a time, like:


    #
    !
    /
    b
    i
    n
    /
    b
    a
    s
    h


    You cannot have multi-dimensional arrays in bash, but if an array element is a string you can deal with the individual characters in a string. For more on arrays:

    Arrays

    Remember: bash is whitespace sensitive!!!! Also, if you run this script on itself notice what happens with the asterisk on line 10 -- demonstrating shell scripts can be dangerous in unpredictable ways (this one isn't dangerous).
    Last edited by MK27; 03-01-2010 at 07:26 PM. Reason: eensy weensy minor error in code
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  2. syntax error when defining vectors
    By starkhorn in forum C++ Programming
    Replies: 5
    Last Post: 09-22-2004, 12:46 PM
  3. Character handling help
    By vandalay in forum C Programming
    Replies: 18
    Last Post: 03-29-2004, 04:32 PM
  4. Game structure, any thoughts?
    By Vorok in forum Game Programming
    Replies: 2
    Last Post: 06-07-2003, 01:47 PM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21