Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

storing grep output to variable

by Anonymous Monk
on Jun 08, 2004 at 15:30 UTC ( #362390=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I think that this bit of code should be able to take output from my grep command and store it in a variable (and in fact uses the same syntax as some previous posts) but for some reason gives me an error message, saying that 'end of file' is unexpected. Any idea what I'm doing wrong? thanks! $info = `grep "$prop" <FILE>`;

Comment on storing grep output to variable
Re: storing grep output to variable
by pelagic (Curate) on Jun 08, 2004 at 15:37 UTC
    Your problem are the double-quotes inside the single-quotes.
    $prop is probably used as searchstring with the $-sign.

    Update
    Or are you interested in the matched lines?
    my $info = grep "$prop", <FILE>; # or my @lines = grep "$prop", <FILE>;

    pelagic
Re: storing grep output to variable
by pbeckingham (Parson) on Jun 08, 2004 at 15:38 UTC

    You are mixing Perl and a utility program in an odd way. Your example looks like it's trying to feed grep a slurp, and you want the file name, not the file contents. Either try:

    my $output = `/usr/bin/grep "$prop" /path/to/file`;
    Or try applying a regular expression to the contents of the file using only Perl.

      I could easily be doing something very odd - I only started learning Perl a few days ago. Is grep not a Perl function?
      What I am trying to do is to retrieve the line of my file that contains the variable $prop, which is fed in on the command line, and store that entire line in a variable. When I try implementing the code that pelagic sent, I get the entire file output, instead of just the one line I want - odd since the input string is unique.

        Yes, grep is a Perl function, but you are doing this:

        `grep ...`
        and the backtick operators invoke a shell to run grep, which is also a Unix system utility.

Re: storing grep output to variable
by blueAdept (Beadle) on Jun 08, 2004 at 16:09 UTC
    You're using the shell utility "grep" by virtue of the backtick quoting - not the perl function "grep" which is similar. Here is is a chunk of perl code that will do a case sensisive grep of a file.

    open(IN, $ARGV[0]); @foo = grep { m/$ARGV[1]/ } <IN>; print @foo;
      Thanks very much everyone! I have gotten the code to work! I went with the following coding:
      my @lines = grep /$prop/, <FILE>;
      This seems to give the result I was looking for, though it's not exactly identical to any of the great solutions you all gave. Anyway thanks for curing my ignorance of grep - at least partially :)
        That's the solution I would use myself. I'd just like to make a note that FILE type filehandles are global. Instead of globals you should use a lexical filehandle: open my $file, '<', $filename or die $!. See perldoc -f my and perldoc perlsub (Private Variables via my()) for an explanation of globals and lexicals.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://362390]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (12)
As of 2014-12-26 18:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (174 votes), past polls