P
Pedro Graca
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;
}
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;
}