Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > newbie dealings with OS "grep -P"

Reply
Thread Tools

newbie dealings with OS "grep -P"

 
 
Pedro Graca
Guest
Posts: n/a
 
      11-14-2003
My system grep does not support the -P option (perl regexp pattern).
So I thought it would be a good idea to make a perl script for that
(after all, only Perl parses perl, right?)

my first working attempt is grepp1 at the bottom


And then I realized this wouldn't allow me to pipe things to grepp1,
so out comes the second attempt (grepp2)


By now I'm using grepp2 very happily until I needed a case sensitive
match
After a tough time, this came out:
$ cat grepp3
#!/usr/bin/perl -nsw
# use strict; ## BEGIN does not like strict

# grepp [-I] pattern [file1 [file2 [...]]]

BEGIN {
$expr = shift; ## need error checking!!!
$regex = qr/$expr/i;
{ no warnings; ## -I might not have been specified
if ($I) { $regex = qr/$expr/; }
} # end no warnings
$\ = "\n"; ## nice little trick
}

chomp;
print if /$regex/;


Now I'm working on grepp4, trying to add options from the 'real' grep.
+ -c == only count matches
+ -H == print the file name
+ -n == print the line number
+ -V == print version and exit

but I don't like having the "use strict;" out.

Is it better to manually shift all parameters and verify if they're
options or the pattern or filenames?

Did I do something wrong with the "perl -s", "use strict;" and "BEGIN"
block?




=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
$ cat grepp1
#!/usr/bin/perl -w
use strict;

# grepp pattern file

my $pt = shift; ## need error checking!!!!
my $fn = shift; ## need error checking!!!!

open (my $FILE, '<', $fn) || die $!;
while (my $line = <$FILE>) {
chomp $line;
print "$line\n" if $line =~ /$pt/i;
}


$ cat grepp2
#!/usr/bin/perl -nw
use strict;

# grepp pattern [file1 [file2 [...]]]

my $pt = shift; ## need error checking!!!!

$\ = "\n"; ## nice little trick
while (<>) {
chomp;
print if /$pt/i;
}

--
@ "=qw f Jt ss x_x am os hd q y_y Pd rk z_z Hab kd q f; $
_ =join "",(map ++ $ _, @ "); s/1/ /g and print; print $/
 
Reply With Quote
 
 
 
 
Bob Walton
Guest
Posts: n/a
 
      11-14-2003
Pedro Graca wrote:

> My system grep does not support the -P option (perl regexp pattern).
> So I thought it would be a good idea to make a perl script for that
> (after all, only Perl parses perl, right?)
>
> my first working attempt is grepp1 at the bottom
>
>
> And then I realized this wouldn't allow me to pipe things to grepp1,
> so out comes the second attempt (grepp2)
>


....

You might check out what the folks at the Unix Reconstruction Project
did with grep:

http://www.perl.com/language/ppt/src/grep/index.html

--
Bob Walton
Email: http://bwalton.com/cgi-bin/emailbob.pl

 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      11-14-2003
Pedro Graca <(E-Mail Removed)> wrote:
> #!/usr/bin/perl -nsw
> # use strict; ## BEGIN does not like strict


There is no problem with BEGIN and strictures. You do have to make
sure all variables are declared before you use them: if you are going
to use them both inside and outside the BEGIN block then the
declaration must come before the BEGIN block. So here you would need:

> # grepp [-I] pattern [file1 [file2 [...]]]


my ($expr, $regex);

> BEGIN {
> $expr = shift; ## need error checking!!!
> $regex = qr/$expr/i;
> { no warnings; ## -I might not have been specified


Doesn't matter... undef is perfectly false and won't give a warning
here. $expr might, though, if you don't specify an expression.

> if ($I) { $regex = qr/$expr/; }
> } # end no warnings
> $\ = "\n"; ## nice little trick


Another nice trick is -l, which does the chomp as well.

> }
>
> chomp;
> print if /$regex/;
>
>
> Now I'm working on grepp4, trying to add options from the 'real' grep.
> + -c == only count matches
> + -H == print the file name
> + -n == print the line number
> + -V == print version and exit
>
> but I don't like having the "use strict;" out.


That is a good instinct. Cultivate it...

> Is it better to manually shift all parameters and verify if they're
> options or the pattern or filenames?


I would suggest that it would be better to use one of the Getopt
modules from CPAN, probably Getopt::Std.

> Did I do something wrong with the "perl -s", "use strict;" and "BEGIN"
> block?


If you use -s, then the variable it creates are globals, so you must
declare them with 'our' rather than with 'my'.

Ben

--
'Deserve [death]? I daresay he did. Many live that deserve death. And some die
that deserve life. Can you give it to them? Then do not be too eager to deal
out death in judgement. For even the very wise cannot see all ends.'
:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-: http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      11-14-2003
Pedro Graca <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> My system grep does not support the -P option (perl regexp pattern).
> So I thought it would be a good idea to make a perl script for that
> (after all, only Perl parses perl, right?)
>
> my first working attempt is grepp1 at the bottom
>
>
> And then I realized this wouldn't allow me to pipe things to grepp1,
> so out comes the second attempt (grepp2)
>
>
> By now I'm using grepp2 very happily until I needed a case sensitive
> match
> After a tough time, this came out:
> $ cat grepp3
> #!/usr/bin/perl -nsw
> # use strict; ## BEGIN does not like strict


BEGIN and strict have no problems with each other. Just declare your
variables, as strict requires.

>
> # grepp [-I] pattern [file1 [file2 [...]]]
>
> BEGIN {
> $expr = shift; ## need error checking!!!
> $regex = qr/$expr/i;
> { no warnings; ## -I might not have been specified
> if ($I) { $regex = qr/$expr/; }
> } # end no warnings
> $\ = "\n"; ## nice little trick


....but only needed if you insist in chomp()ing the input.

> }
>
> chomp;
> print if /$regex/;
>
>
> Now I'm working on grepp4, trying to add options from the 'real' grep.
> + -c == only count matches
> + -H == print the file name
> + -n == print the line number
> + -V == print version and exit
>
> but I don't like having the "use strict;" out.
>
> Is it better to manually shift all parameters and verify if they're
> options or the pattern or filenames?
>
> Did I do something wrong with the "perl -s", "use strict;" and "BEGIN"
> block?


Well, you need to declare your variables with strict. A first attempt
would look like this (I'm leaving the -I flag out, that's not the problem).
Note that this code does *not* work:

#!/usr/local/bin/perl -nsw

my $regex; # we need that outside the BEGIN block
BEGIN {
my $expr = shift;
$regex = qr/$expr/;
}

print if /$regex/;

The problem is that -n constructs a loop around the whole script,
which means that the run-time effect of "my $regex" happens each
time around the loop. But the run-time effect of my() is to clear
the variable, so $regex is not set when it is needed.

The easiest way out is to use a package variable for $regex. So a
working variant of your script would be:

#!/usr/local/bin/perl -nsw
use strict; ## BEGIN does not like strict

# grepp [-I] pattern [file1 [file2 [...]]]

our $regex;
BEGIN {
my $expr = shift; ## need error checking!!!
$regex = qr/$expr/i or die "invalid pattern: /$expr/"; # like this?
if (our $I) {
$regex = qr/$expr/;
}
}

print if /$regex/;

A better solution would be to write the loop yourself and keep
"my $regex" out of it. BEGIN is just a trick to keep some code
out of the loop, but it doesn't work very well in this case.

You may want to take a look at the _Perl Cookbook_, which devotes
a whole chapter to grep-like programs. (The first edition did,
I guess the second one still does.)

[more grep's snipped]

Anno
 
Reply With Quote
 
Pedro Graca
Guest
Posts: n/a
 
      11-14-2003
Bob Walton wrote:
> You might check out what the folks at the Unix Reconstruction Project
> did with grep:
>
> http://www.perl.com/language/ppt/src/grep/index.html


Thank you. I've looked at it and got a few good new ideas for my
project.

--
..sig
 
Reply With Quote
 
Pedro Graca
Guest
Posts: n/a
 
      11-14-2003
Ben Morrow wrote:
> Pedro Graca <(E-Mail Removed)> wrote:
>> #!/usr/bin/perl -nsw
>> # use strict; ## BEGIN does not like strict

>
> There is no problem with BEGIN and strictures. You do have to make
> sure all variables are declared before you use them: if you are going
> to use them both inside and outside the BEGIN block then the
> declaration must come before the BEGIN block. So here you would need:
>
>> # grepp [-I] pattern [file1 [file2 [...]]]

>
> my ($expr, $regex);


Does not work. Have to make them 'our'
our ($expr, $regex, $I);

>> BEGIN {
>> $expr = shift; ## need error checking!!!
>> $regex = qr/$expr/i;
>> { no warnings; ## -I might not have been specified

>
> Doesn't matter... undef is perfectly false and won't give a warning
> here. $expr might, though, if you don't specify an expression.


Problem was with $I. When I tried
grepp -I pattern file
there would be no warning, but with
grepp pattern file
I'd get
Name "main::I" used only once: possible typo at ...
[ grepp symlinks to the latest greppn ]

Now, with $I declared with 'our' everything is ok

.... well, if I try grepp -X it complains "main::X" used only once

hopefully I'll have that dealt with in grepp4 (or maybe grepp57)

>> $\ = "\n"; ## nice little trick

>
> Another nice trick is -l, which does the chomp as well.


two more lines off of my code.

>> but I don't like having the "use strict;" out.

>
> That is a good instinct. Cultivate it...


It's back on, right where it should!

> I would suggest that it would be better to use one of the Getopt
> modules from CPAN, probably Getopt::Std.


Thank you. That module does not get installed by default, but dealing
with command-line options is such a daunting task I do not feel like
reinventing the wheel for that.

>> Did I do something wrong with the "perl -s", "use strict;" and "BEGIN"
>> block?

>
> If you use -s, then the variable it creates are globals, so you must
> declare them with 'our' rather than with 'my'.


Ah! that's it!


Thank you very much for your answers.

--
..sig
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Anyone Have Dealings With External Enclosures? Kyle :o\) Computer Support 3 07-20-2006 03:01 AM
another newbie question from another newbie.... Lee UK VOIP 4 05-17-2005 04:10 PM
anyone had any dealings with microglobe.co.uk tbm Digital Photography 1 04-03-2005 12:36 PM
newbie: cisco vlan newbie question No Spam Cisco 3 06-07-2004 10:02 AM
Newbie! I'm a newbie! What's wrong with this program? Id0x Python 4 07-20-2003 11:40 PM



Advertisments