Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Same, tho it'd be interesting to see if this behavior holds if the file ends without a trailing newline and your match is on the final newline-less line.



Fortunately, it's pretty simple to test.

    $ printf 'Line with EOL\nLine without EOL' | grep 'EOL$'        
    Line with EOL
    Line without EOL
    $ grep --version | head -n1
    grep (GNU grep) 3.8


The line does end with the file, so it's logically consistent.

It's not matching the newline character after all.


Yes exactly, they match the end of a line, not a newline character. Some examples from documentation:

man 7 regex: '$' (matching the null string at the end of a line)

pcre2pattern: The circumflex and dollar metacharacters are zero-width assertions. That is, they test for a particular condition being true without consuming any characters from the subject string. These two metacharacters are concerned with matching the starts and ends of lines. ... The dollar character is an assertion that is true only if the current matching point is at the end of the subject string, or immediately before a newline at the end of the string (by default), unless PCRE2_NOTEOL is set. Note, however, that it does not actually match the newline. Dollar need not be the last character of the pattern if a number of alternatives are involved, but it should be the last item in any branch in which it appears. Dollar has no special meaning in a character class.


Thanks! I was AFK and didn't have a grep (or a shell) handy on my phone.




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: