From: Zhang Weiwu on
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.*;' .

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'";

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)


From: Camaleón on
On Mon, 02 Aug 2010 14:56:45 +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.

(...)

Google says there is a package named "pcregrep" that it may help with
this :-?

Curiously, "man grep" tell about "-P" swicth but it seems to be disable
for Debian package build:

***
grep: Support for the -P option is not compiled into this --disable-perl-
regexp binary
***

Greetings,

--
Camaleón


--
To UNSUBSCRIBE, email to debian-user-REQUEST(a)lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster(a)lists.debian.org
Archive: http://lists.debian.org/pan.2010.08.02.11.27.12(a)gmail.com
From: Karl Vogel on
>> On Mon, 02 Aug 2010 14:56:45 +0800,
>> Zhang Weiwu <zhangweiwu(a)realss.com> said:

Z> I'm grepping a bunch of files each have a segment code that executes a
Z> SQL. My problem is that the query spans across several lines and I
Z> can't seem to make grep honor (?s) for that.

Perl Is Our Friend. Here's some text to search:

me% cat -n sample
1 Message-ID: <4C566C2D.7000206(a)realss.com>
2 Date: Mon, 02 Aug 2010 14:56:45 +0800
3 From: Zhang Weiwu <zhangweiwu(a)realss.com>
4 Organization: Real Softservice
5 Status: RO
6 Content-Length: 2410
7 Lines: 80
8
9 I'm grepping a bunch of files each have a segment code
10 that executes a SQL. Both selects should match:
11
12 select * from table1 where id=1;
13
14 select * from table2 where id=1
15 and name='foo'";
16
17 I tried to use -z parameter for grep, which the manual says
18 would make grep not treating \n as line terminator. But
19 it doesn't work neither. A simple test shows I might have
20 misunderstood the use of -z:

The script below my signature gives these results:

me% ./pgrep 'select.*from.*;' sample
[sample:12] select * from table1 where id=1;
matched >>select * from table1 where id=1;<<

[sample:15] select * from table2 where id=1
and name='foo'";
matched >>select * from table2 where id=1
and name='foo'";<<

"[sample:15]" means the match happened at or before line 15 in file
"sample". You can pass multiple files on the command line. The
"matched" stuff is there in case there's some distracting text on the
line besides the select statement. If you want matching to be
case-sensitive, change "si:" to "s:" on the line where $pattern is set.

--
Karl Vogel I don't speak for the USAF or my company

---------------------------------------------------------------------------
#!/usr/bin/perl -w
# Taken from perl-grep3.pl in "Mastering Perl"

use strict;

# Get the desired pattern, make newlines match '.' and ignore case.
my $pattern = shift @ARGV || die "I need a pattern\n";
$pattern = '(?si:' . $pattern . ')';

# Make sure pattern works.
my $regex = eval { qr/$pattern/ };
die "Check your pattern! $@" if $@;

# Use paragraph mode to handle newlines.
$/ = "";
my $line = 0;

while (<>) {
$line += tr/\n/\n/;
chomp;
print "[$ARGV:", $line-1, "] $_\n\t\tmatched >>$&<<\n\n" if m/$regex/;
$line = 0 if eof(ARGV); # reset counter for a new file.
}

exit(0);


--
To UNSUBSCRIBE, email to debian-user-REQUEST(a)lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster(a)lists.debian.org
Archive: http://lists.debian.org/20100802214030.A21B6BF2A(a)kev.msw.wpafb.af.mil
From: Andre Majorel on
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."


--
To UNSUBSCRIBE, email to debian-user-REQUEST(a)lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster(a)lists.debian.org
Archive: http://lists.debian.org/20100803095328.GA19284(a)aym.net2.nerim.net
From: Zhang Weiwu on
On 2010年08月03日 17:53, Andre Majorel wrote:
>> > $ 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.
>

No it doesn't.

$ printf 'a\nb' | grep -z 'a.*b'
$