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

Submitter:  None
Submitted:  Tue 23 Oct 2007 01:57:24 PM UTC
   
 
Category:  None Severity:  3 - Normal
Item Group:  None Status:  Invalid
Privacy:  Public Assigned 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:
00000000  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
00000000  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

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

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

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

     

    Follow 2 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2010-02-06 bonzini StatusNone Invalid
        Open/ClosedOpen Closed

    Back to the top

    Powered by Savane 3.13-02a9.
    Corresponding source code