Operating System - OpenVMS
1752565 Members
5852 Online
108788 Solutions
New Discussion юеВ

Problem regarding use of grep in perl script

 
Ajay Saini
Frequent Advisor

Problem regarding use of grep in perl script

Hi everybody!
I'm facing a problem in a perl script wherein I want to grep those lines from a file which start with "["(open square bracket).I have used two ways:-
-@list=`grep '^\[' filename`;
-system "grep '^\[' filename";
In both cases I'm getting the error message:
grep: [] imbalance
What is wrong with them and how to remove the problem??
4 REPLIES 4
Joseph Huber_1
Honored Contributor

Re: Problem regarding use of grep in perl script

It probably very much depends how Your system (I assume VMS, since we are in a VMS forum) defines the command grep.
In my system, grep is GNU grep from the GNV suite, and it works under the bash shell:
tmp.txt contains "[ at first position":

bash
bash$ grep '^\[' tmp.txt
[ at first position
bash$ exit
exit

If grep is invoked from DCL:

grep '^\[' tmp.txt
grep.exe: Unmatched [ or [^

which probably is the same as "[] imbalance".

grep ^\[ tmp.txt !Note without quotes!
[ at first position

I.e. works without quotes!

grep --version
grep (GNU grep) 2.4.2 Rev 1.4 (9-Jul-2001)


Now I'm no perl expert, but for it looks strange to call external grep out of perl: perl's stength is string handling. Hein will surely present a one-liner doing the right thing :-)


http://www.mpp.mpg.de/~huber
Hoff
Honored Contributor

Re: Problem regarding use of grep in perl script

What's wrong? Well, the use of grep here (at all) seems rather wrong. Perl has very capable regular expression processing.

The lack of a problem statement (and the lack of platform and version information) is also somewhat of a problem. With that, we'd know if this is perl on OpenVMS, or some other OS platform.

Using regex to parse an OpenVMS filename seems wrong, too; the OpenVMS filename syntax is entirely and thoroughly arcane, and does not lend itself to a regex. (You know that < and > are legal syntax here, for instance? Yes; the folks that wrote the PCSI tool didn't know that, either.)

I'd start with one of the perl libraries for Perl and for Perl on VMS here, and see if there were parsing tools there. Here are a couple:

http://perldoc.perl.org/File/Basename.html
http://search.cpan.org/dist/perl/vms/ext/Filespec.pm


And I'd probably hook Perl to the RMS parsing routines long before I tried regex, either. RMS knows the rules. Having done (and regretted doing) parsing in code, it's best to punt this down to OpenVMS to handle.

Here's one of many sites that discusses escaping reserved characters:

http://tools.devshed.com/c/a/Web-Development/Beginning-Perl-Part-2-Escaping-Special-Characters/

And:

http://forums11.itrc.hp.com/service/forums/questionanswer.do?threadId=692014

Why are you writing a filename parser? Chances are, that's already been written and debugged by somebody. (Is this homework? If so, you have an interesting instructor for your class; this particular choice is a very good choice for an assignment for learning how to escape reserved characters. And if the assignment was specifically to use grep here via a Unix bash or via GNV, that was a particularly devious choice for the assignment , as you have to deal both with perl and its processing and with grep and its processing; you'll very likely need to double up the escapement here.)


Craig A Berry
Honored Contributor

Re: Problem regarding use of grep in perl script

Hmm. Seems improbable that I'm getting here before Hein for once. But yes, this is a trivial problem with a Perl one-liner:

$ type foo.txt
line
[line starting with bracket
another line
$ perl -nle "print if /^\[/;" < foo.txt
[line starting with bracket


And as Joseph demonstrated, it's pretty darn simple with just grep. The OP may well be having trouble with shell quoting rather than either Perl or grep, but without knowing what shell is in use that's hard to say.
Hein van den Heuvel
Honored Contributor

Re: Problem regarding use of grep in perl script

Craig>> Seems improbable that I'm getting here before Hein for once

Been busy... :-)

It also seems silly to me to ask the shell to activate 'grep' when perl has a perfectly find grep function build in !

Just some silly examples:

$ perl -e "@files=<*.*>; for (@files) { print qq($_\n)}"


perl -e "open F,'; @nocomment = grep /^\$\s+[^!]/,@login; print for (@nocomment)"

Hein.