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.
Asked
Active
Viewed 2,565 times
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.
garyjohn
- 34,610
- 8
- 97
- 89
0
I think you need to use awk instead.
awk has RS (Records Seperator) variable to change the line terminator
Gaurav Joseph
- 1,725
- 13
- 24
mug896
- 159
- 1
- 3