Grep Whitespace across multiple lines

2

Given the following in a bunch of text files (.vb):

Partial Class [A-Za-z0-9_]
  Inherits System.Web.UI.Page

End Class

I'm trying to grep for files that have this basically empty code file, to generate a list of these files, and delete them.

grep "Inherits System.Web.UI.Page[:space:]*End Class" -r

However the above grep doesn't work... And after reading through the POSIX character classes and grep man pages, I'm stumped

enorl76

Posted 2015-04-10T13:39:28.010

Reputation: 272

Answers

1

By default, grep matches only single lines. But you can use the -z (--null-data) option to force it to treat the input as a set of lines:

grep -Pzo -r "Inherits System.Web.UI.Page(\s|\n)*End Class" *

Another option is using the pcregrep with the -M option, like this:

pcregrep -M 'Inherits System.Web.UI.Page(\n|\s)*End Class'

That Brazilian Guy

Posted 2015-04-10T13:39:28.010

Reputation: 5 880

grep always treats the input as a set of lines. Here's the full quote from the man page: "Treat the input as a set of lines, each terminated by a zero byte (the ASCII NUL character) instead of a newline." So, -z makes it treat the input as a set of null-terminated lines rather than newline-terminated lines.

– clacke – 2015-12-17T16:13:34.480