Find the answer to your Linux question:
Results 1 to 8 of 8
Hi, I am using Free BSD system and am investigating the usage of diff and cmp. I have two files, and the only difference is mainly in the path. I ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    May 2013
    Posts
    4

    diff vs cmp compare question


    Hi, I am using Free BSD system and am investigating the usage of diff and cmp.

    I have two files, and the only difference is mainly in the path. I have searched the man pages for both diff and cmp, and have not found a way to ignore the path.

    For example,

    One file has a line with this path:

    /usr/local/bin/R0028/Output234.txt

    and the other has a line with:

    /usr/local/bin/S0028/Output234.txt

    It does not matter what the actual path name of the files are. I am only interested in the differences of the text in the files, ignoring the path listed in the files.

    Both files have over one hundred lines, and I wish to be able to find out differences other than the path.

    There obviously will be more differences in these files other than the path, but I wish to be able to ignore the path differences and concentrate on the other text differences.

    Thank you.
    Last edited by gedinfo; 05-29-2013 at 04:12 AM. Reason: more clarity

  2. #2
    Linux Enthusiast
    Join Date
    Jan 2005
    Location
    Saint Paul, MN
    Posts
    668
    Diff is for text files, while cmp reports the byte location that are different (mostly for binary files). Both of these are contents of the file and not the qualified_filenames passed are arguments.

    Are you attempting to get the file being referenced inside a file? Or are doing:

    diff /usr/local/bin/R0028/Output234.txt /usr/local/bin/S0028/Output234.txt


    Also not that the VIM package also has a command "vimdiff" (or if you have installed gvim, gvimdiff) that splits the terminal into left and right and uses folding to show the differences (used diff under the hood).

    To get another answer, you might need to explain better (maybe an example) of what you are doing.

  3. #3
    Just Joined!
    Join Date
    May 2013
    Posts
    4
    Each file contains a grep listing, so file A contains grep listings from an older build, and file B contains grep listings from the current build.

    Therefore, one major difference is the build number contained in the pathname of the grep output.

    I know that there will be a difference between build numbers, so I am only concerned with possible differences between the files not relating to path.

    Example:

    File A contains:
    //usr/local/bin/R0028/Output234.txt -grep listing one two three

    and File B contains
    /usr/local/bin/S0028/Output234.txt - grep listing four five six

    I only am interested in the one two three/four five six differences.

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Enthusiast
    Join Date
    Jan 2005
    Location
    Saint Paul, MN
    Posts
    668
    I still do not understand what you have for data does file A look like:
    [code]
    /usr/local/bin/R0028/Output234.txt
    one: listing line 2
    two: listing line 2
    two: listing line 3
    three: listing line 2
    three: listing line 4
    [code]
    or is the file A:
    Code:
    /usr/local/bin/R0028/Output234.txt -grep listing one two three
    and you wish to extract the names:
    • one
    • two
    • three

    and similar for file B and you desire to end up with a script such as:
    Code:
    diff one four
    diff two five
    diff three six

  6. #5
    Just Joined!
    Join Date
    May 2013
    Posts
    4
    File A and File B do not matter what path they reside in.

    File A and File B contain grep listings from a group of files within a directory.

    I do not know how to explain this further:


    File A has a line in it containing a path.
    File B has a line in it containing a path.

    Each line is obviously different, because File A refers to a prior build, and File B refers to a current build.

    There may be other items on the same line, which do not match.

    I am NOT interested in the path differences, only the items appearing after the path information.

  7. #6
    Linux Enthusiast
    Join Date
    Jan 2005
    Location
    Saint Paul, MN
    Posts
    668
    Okay, I will ask another question...
    You have a file A that contains:
    Code:
    /usr/local/bin/R0028/Output234.txt -grep listing one two three
    and a file B that contains:
    Code:
    /usr/local/bin/S0028/Output234.txt - grep listing four five six
    But you would like the compare to see:
    Code:
    -grep listing one two three
    and
    Code:
    - grep listing four five six
    How would that be performed?

    Is this correct description of your problem?

    If so, this would do what you want.
    Code:
    bash$ diff <(cut -d\  -f2- file_A) <(cut -d\  -f2- file_B)
    1c1
    < -grep listing one two three
    ---
    > - grep listing four five six
    bash$
    diff does not do any filtering, it assumes if you like to do filtering of the file that you would do that and give that filtered file to diff. Often "sed" is used to do the filtering such as:
    Code:
    bash$ diff <(sed 's=^/.*/[/*/]*==' file_A) <(sed 's=^/.*/[.*/]*==' file_B)
    1c1
    < Output234.txt -grep listing one two three
    ---
    > Output234.txt - grep listing four five six
    bash$
    Last edited by alf55; 05-30-2013 at 05:07 AM. Reason: Add solution of this is the problem

  8. #7
    Just Joined!
    Join Date
    May 2013
    Posts
    4
    Using sed was the trick.

    Thank you.

  9. #8
    Linux Enthusiast
    Join Date
    Jan 2005
    Location
    Saint Paul, MN
    Posts
    668
    Glad that I could answer your question (after I figured out what you were attempting to ask).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •