Welcome to the Linux Foundation Forum!

compare files whose lines have been broken

Example, first file :

1234 56asdfh asdfauiiiiiiil asdf

Second file :

1234 56asdfh
asdfauiiiiiiil
asdf


I would like to compare these two files and find no difference. I want to ignore all the breaking lines

Is there a command or a tool that can do that ? I want to use it in a batch file.

Thanks :)

Comments

  • mfillpot
    mfillpot Posts: 2,177
    This is a very difficult issue that I have been attempting to build a resolution on since the question was originally asked. It seems to me that the logic used in the absnse of line breaks makes this extrememly painful. If you remove all line breaks then the two files are all single line files, all diff utils that I have used to comparisons based upon line numbers, so they would not work. I beleive to otain your goal it would be best for you to write a bash script that first removes the line breaks, then increments one character at a time and reports the differences. However this would still be flawed because one a single space is detected it would then report all further characters and changes since the characters in the specific locations do not match. You may want to add some logic stating that if the character was replaced with a space to evaluate the non-space character against the next character in the opposite file.
  • woboyle
    woboyle Posts: 501
    Since the diff utility is line oriented, this is difficult. It has options to ignore white space, but I don't know of a way to make it ignore lines entirely.
  • you could use output each of those files into a files that have
    a single line only, then compare the one-line files.
    In other words, you replace the new line character by one space and you
    remove extra spaces.

    A seder could do that for you.
    Example

    ###copy and paste to seder.sed below this line
    #!/bin/sed -f
    
    :loop
    N
    $!b loop
    
    s?[ \t\n]\{1,\}? ?g
    
    #### end of copy ####

    After you pasted this to a sed script file (eg seder.sed) you make the
    script executable:
    chmod +x seder.sed
    

    Now, let's say the two files you want to compare are file1.txt and file2.txt.
    You run these commands:
    ./seder.sed file1.txt > firstfile
    ./seder.sed file2.txt > secondfile
    diff firstfile secondfile
    
  • mfillpot
    mfillpot Posts: 2,177
    vonbiber,
    This is a nice script. I definitely would like to hear back from the original requester to see if this fulfills their needs.

Categories

Upcoming Training