Reputation: 133
I have some files with the text:
xxxxx
xxxxx
<cert>
</cert>
some other stuff
How can I search with grep and ignore the line returns? I have many files in the same folder. I have tried this but it does not seem to stop running:
tr '\n' ' ' | grep '<cert></cert>' *
Upvotes: 1
Views: 461
Reputation: 54465
That is searching for a multi-line pattern, which the usual grep
does not appear to support. There are alternative tools, e.g.,
However, GNU grep is said to support this as well:
How do I grep for multiple patterns on multiple lines? gives as an example
grep -Pzo "^begin\$(.|\n)*^end$"
file
to use a newline in a pattern. The options used however include the "experimental" -P
which may make it less suitable than pcregrep
:
-P, --perl-regexp
Interpret PATTERN as a Perl regular expression. This is highly experimental and grep -P may warn of unimplemented features.
-z, --null-data
Treat the input as a set of lines, each terminated by a zero byte (the ASCII NUL character) instead of a newline. Like the -Z or --null option, this option can be used with commands like sort -z to process arbitrary file names.
-o, --only-matching
Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.
Some experimental options are useful, others less so. This one was noted as the source of problems in Searching for non-ascii characters.
Upvotes: 1