How can I make grep use CRLF (and not LF) as a newline?

Posted on


I need to grep through a file where not only are lines terminated by a CRLF, but they might also have several LFs on each line. How can I make grep ignore LFs unless there’s a CR in front of them? FYI, I’m using OS X, so it would be helpful to have BSD grep instructions.


I don’t see any way to do that using just grep, but you could use perl, e.g.,

perl -e '$/="rn";' -ne 'print if /your_pattern/;' your_filename

or you could use tr as a filter around grep, e.g.,

tr 'nr' 'n' < your_filename | grep -a your_pattern | tr 'n' 'nr'

The latter assumes that your file doesn’t already contain any NULs.

I think you need to use awk instead.
awk has RS (Records Seperator) variable to change the line terminator

Leave a Reply

Your email address will not be published.