1

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.

Jason Baker
  • 8,932

2 Answers2

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.

garyjohn
  • 36,494
0

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

mug896
  • 199