Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

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

perl -e '$/="\r\n";' -ne 'print if /your_pattern/;' your_filename

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

tr '\n\r' '\0\n' < your_filename | grep -a your_pattern | tr '\0\n' '\n\r'

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

share|improve this answer

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

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.