buggrep - Bugs: bug #21401, Newline inserted by grep at EOF...

 
 

You are not allowed to post comments on this tracker with your current authentication level.

bug #21401: Newline inserted by grep at EOF causes false positives

Submitted by:  None
Submitted on:  Tue 23 Oct 2007 01:57:24 PM UTC  
 
Category: NoneSeverity: 3 - Normal
Item Group: NoneStatus: Invalid
Privacy: PublicAssigned to: None
Open/Closed: Closed

Sat 06 Feb 2010 01:08:45 PM UTC, comment #1:

grep operates by reading one line from the input stream, removing any trailing newline, and matching the pattern against it. The fact that grepping \x0a works is actually a bug in grep -P (in fact it doesn't work with grep/grep -E/grep -F).

Paolo Bonzini <bonzini>
Tue 23 Oct 2007 01:57:24 PM UTC, original submission:

I have the following file:

Line 1\r\n
Line 2

The first line has a DOS newline, while the second has no newline at all.

For information, the hexdump of the file is:

                1. 4c 69 6e 65 20 31 0d 0a 4c 69 6e 65 20 32

0000000e

I want to know if the file contains Unix newlines (\n).
Therefore, I run grep on it in the following way:

$ grep -P '[^\x0d]\x0a' file.txt
Line 2

The problem is that grep thinks that Line 2 matches, which is definitely not the case.

The following experience pinpoints the problem:

$ grep -P '[^\x0d]\x0a' file.txt | hexdump -C

                1. 4c 69 6e 65 20 32 0a

00000007

grep has apparently inserted a newline automatically at the end of the file, using the Unix format, which then matches my regexp.

So grep should find not match in the file, and it finds one.
It's a corner case, but it's an bug, even an annoying bug if one is troubleshooting newline issues.

Anonymous

 

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -unavailable- added by bonzini (Posted a comment)
  •  

    Do you think this task is very important?
    If so, you can click here to add your encouragement to it.
    This task has 0 encouragements so far.

    Only logged-in users can vote.

     

    Please enter the title of George Orwell's famous dystopian book (it's a date):

     

     

    Follow 2 latest changes.

    Date Changed By Updated Field Previous Value => Replaced By
    Sat 06 Feb 2010 01:08:45 PM UTCbonziniStatusNone=>Invalid
      Open/ClosedOpen=>Closed

    Back to the top


    Powered by Savane 3.1-cleanup1