trim the last blank-line and compare files 
Author Message
 trim the last blank-line and compare files

Hi

Im comparing two files. Newer file has blank-line introduced at the
end. So the file difference is only a blank-line at the end. If I need
to ignore this difference, module File::Compare does not help.

tried with the third argument to the cmp function:
  cmp("file1","file2", sub { chomp($_[0]) ne chomp($_[1]) })
which would chomp the lines and then compare the difference if any.
But still there is difference.

Can you please let me know where Im making the mistake.

Thanks
Subhash



Sat, 18 Aug 2012 13:32:09 GMT  
 trim the last blank-line and compare files

Quote:
> Hi

> Im comparing two files. Newer file has blank-line introduced at the
> end. So the file difference is only a blank-line at the end. If I need
> to ignore this difference, module File::Compare does not help.

> tried with the third argument to the cmp function:
> ? cmp("file1","file2", sub { chomp($_[0]) ne chomp($_[1]) })
> which would chomp the lines and then compare the difference if any.
> But still there is difference.

> Can you please let me know where Im making the mistake.

> Thanks
> Subhash

tweaked a bit, added chomp to a subroutine:
sub trim($) {
        my $line = $_[0];
        chomp($line);
        return $line;
Quote:
}

my $result = cmp("file1","file2", sub {trim $_[0] ne trim $_[1]});

but still the comparison fails



Sat, 18 Aug 2012 15:49:19 GMT  
 trim the last blank-line and compare files

Quote:
> Im comparing two files. Newer file has blank-line introduced at the
> end. So the file difference is only a blank-line at the end.

Tie::File lets you treat a file like an array. You might pop the last
line of the file you are treating like an array. I'll try this later
on in the day when I get a break and post back.

CC.



Sat, 18 Aug 2012 15:49:45 GMT  
 trim the last blank-line and compare files


  >>
  >> Im comparing two files. Newer file has blank-line introduced at the
  >> end. So the file difference is only a blank-line at the end. If I need
  >> to ignore this difference, module File::Compare does not help.
  >>
  >> tried with the third argument to the cmp function:
  >> ? cmp("file1","file2", sub { chomp($_[0]) ne chomp($_[1]) })
  >> which would chomp the lines and then compare the difference if any.
  >> But still there is difference.

how are you getting the cmp() function into your code? are you
explicitly importing it? from the module docs:

        File::Compare::cmp is a synonym for File::Compare::compare.  It is
        exported from File::Compare only by request.

use the longer name as you may be getting the builtin cmp op instead.

  S> tweaked a bit, added chomp to a subroutine:
  S> sub trim($) {
  S>         my $line = $_[0];
  S>         chomp($line);
  S>         return $line;
  S> }

are you sure the blank line only has a newline in it? you need to check/trim
all trailing white space if it isn't just a newline.

  S> my $result = cmp("file1","file2", sub {trim $_[0] ne trim $_[1]});

have you run the unix diff command and seen that is the only difference?
how do you know it is only a trailing blank line that is the failing
diff?

uri

--

-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------



Sat, 18 Aug 2012 15:56:18 GMT  
 trim the last blank-line and compare files

Quote:


> ? >>
> ? >> Im comparing two files. Newer file has blank-line introduced at the
> ? >> end. So the file difference is only a blank-line at the end. If I need
> ? >> to ignore this difference, module File::Compare does not help.
> ? >>
> ? >> tried with the third argument to the cmp function:
> ? >> cmp("file1","file2", sub { chomp($_[0]) ne chomp($_[1]) })
> ? >> which would chomp the lines and then compare the difference if any.
> ? >> But still there is difference.

> how are you getting the cmp() function into your code? are you
> explicitly importing it? from the module docs:

> ? ? ? ? File::Compare::cmp is a synonym for File::Compare::compare. ?It is
> ? ? ? ? exported from File::Compare only by request.

> use the longer name as you may be getting the builtin cmp op instead.

> ? S> tweaked a bit, added chomp to a subroutine:
> ? S> sub trim($) {
> ? S> ? ? ? ? my $line = $_[0];
> ? S> ? ? ? ? chomp($line);
> ? S> ? ? ? ? return $line;
> ? S> }

> are you sure the blank line only has a newline in it? you need to check/trim
> all trailing white space if it isn't just a newline.

> ? S> my $result = cmp("file1","file2", sub {trim $_[0] ne trim $_[1]});

> have you run the unix diff command and seen that is the only difference?
> how do you know it is only a trailing blank line that is the failing
> diff?

> uri

> --

> ----- ?Perl Code Review , Architecture, Development, Training, Support ------
> --------- ?Gourmet Hot Cocoa Mix ?---- ?http://bestfriendscocoa.com---------

CC, presently have read the entire file into an array and removing
blank-lines from the last.

Yes Uri, have explicitly imported cmp of File::Compare module.
ran diff under UNIX, it showed the difference in the last line only.
(ie) the blank line

Thanks
Subhash



Sat, 18 Aug 2012 16:33:13 GMT  
 trim the last blank-line and compare files

  S> Yes Uri, have explicitly imported cmp of File::Compare module.
  S> ran diff under UNIX, it showed the difference in the last line only.
  S> (ie) the blank line

but was it just a newline which chomp could handle or were there blanks
in it? diff won't show you that. you need to look carefully at that last
line to see what is in it. then write your trim to handle it.

uri

--

-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------



Sat, 18 Aug 2012 16:38:00 GMT  
 trim the last blank-line and compare files

Quote:

>Im comparing two files. Newer file has blank-line introduced at the
>end. So the file difference is only a blank-line at the end.[...]
>which would chomp the lines and then compare the difference if any.
>But still there is difference.

This is a wild guess only but a blank line even after it is chomp()ed is
still something different than no line (empty string versus EOF or
undef).

Even more: if I were to implement this function then the first thing I
would check is the file size. If the file size is different -and your
second file must be larger by your own description- then don't even
bother comparing the content because the contents cannot possibly be the
same.

The easiest solution might be to append a blank line to the older file,
too, if necessary in a temporary copy, and then compare their contents.

jue



Sat, 18 Aug 2012 18:55:57 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Trimming blank lines from end of file?

2. Trimming blanks

3. Trim trailing blanks...

4. trimming blank

5. To trim right blanks from a field.

6. write is trimming trailing blanks?

7. Trimming blanks

8. how can I read lines from file compare it and write it to another file

9. Deleting (Only) First Blank Line in File

10. SELECT(rbits,wbits,ebits,timeout) adds blank line to file

11. Blank line appending data to file

12. Removing blank lines at beginng/end of a file

 

 
Powered by phpBB® Forum Software