Can grep be used on a Perl variable?

Posted by Structure on Stack Overflow See other posts from Stack Overflow or by Structure
Published on 2010-03-08T09:22:28Z Indexed on 2010/03/08 9:36 UTC
Read the original article Hit count: 321

Filed under:
|

Is it possible one way or another to, within a Perl script, effectively execute grep against a Perl variable? An equivalent Perl function would be equally acceptable, I just want to keep the solution as simple as possible.

For example:

#!/usr/bin/perl
#!/bin/grep

$var="foobar";

$newvar="system('grep -o "foo" $var');

sprintf $newvar;

Where I expect sprintf $newvar to output foo.

Would also welcome any feedback on best practice here. I am not extremely familiar with Perl.

© Stack Overflow or respective owner

Related posts about perl

Related posts about grep