Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

2 Answers 2

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

 
discard

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.