[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: match across line using grep



On 2010-08-02 14:56 +0800, Zhang Weiwu wrote:

> I'm grepping a bunch of files each have a segment code that
> executes a SQL. My problem is that the query spans across
> several lines and I can't seem to make grep honor (?s) for
> that. Here's an example:
> 
> grep --E 'select.*from.*;' .

"--E" ? Did you mean "-E" ?

> so that matches the following fine:
> 
>     select * from mytable where id=1;
> 
> 
> however, it does not match the following:
> 
>     select * from mytable where id=1
>     and name='foo'";

So your search unit is one SQL statement. You need something
that knows SQL syntax and can extract SQL statements from your
file and present them to grep, each on its own line.

If all the semicolons in your SQL code terminate a statement
(E.G. no semicolons in string constants), you might be able to
get away with

  tr '\n;' ' \n'

> I tried to use -z parameter for grep, which the manual says
> would make grep not treating \n as line terminator. But it
> doesn't work neither. A simple test shows I might have
> misunderstood the use of -z:
> 
> $ printf 'a\nb' | grep -zo a.*b
> 
> (The above should output something /if/ -z would make egrep
> not consider \n as string terminator. But it has produced no
> output)

But grep -z does. This would seem to be an undocumented
limitation of -o.

-- 
André Majorel <http://www.teaser.fr/~amajorel/>
"Of course the Debian project would never publish my email address !
Do you think they're stupid ? Spammers would harvest it."


Reply to: