- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - OpenVMS
- >
- Problem regarding use of grep in perl script
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-27-2009 01:51 AM
тАО04-27-2009 01:51 AM
Problem regarding use of grep in perl script
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??
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-27-2009 03:39 AM
тАО04-27-2009 03:39 AM
Re: Problem regarding use of grep in perl script
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 :-)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-27-2009 05:50 AM
тАО04-27-2009 05:50 AM
Re: Problem regarding use of grep in perl script
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.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-27-2009 11:03 AM
тАО04-27-2009 11:03 AM
Re: Problem regarding use of grep in perl script
$ 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-27-2009 08:06 PM
тАО04-27-2009 08:06 PM
Re: Problem regarding use of grep in perl script
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,'
Hein.