Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 09-23-2004, 07:14 PM   #1
LQ Newbie
Registered: Sep 2004
Posts: 11

Rep: Reputation: 0
Need to assign result of grep to var in PERL

I'm in PERL.

I've got a variable, say $x

I want to run a system call, say "grep -c mystring myfile", and assign the result of the grep back to $x.

When I say result of the grep, that would be what one would normally see in stdio... NOT the 0 (success) or errcode of the actual call to grep.

I've tried

$x = system("grep -c mystring myfile");

but that just gives me 0 (indicating no error with the call to grep) rather than my expected count (which in my case is 14).

How do I get the $x to contain the value 14, or whatever the grep would spit out to stdio?

Old 09-23-2004, 07:25 PM   #2
Registered: Sep 2004
Location: Carlisle, MA
Distribution: Debian 8
Posts: 419

Rep: Reputation: 30
$x = `grep -c mystring myfile`;


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Using find and grep - how to exclude a result ? graziano1968 Linux - General 13 12-04-2010 11:02 PM
ps -ef | grep iptables gives no result ? markraem Linux - Networking 1 07-07-2004 06:28 AM
how to grep within a script, and test result? bobbyr Programming 4 01-13-2004 01:11 PM
multiline grep result > multiple variable? mikshaw Linux - Software 5 12-25-2003 11:13 PM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 12:38 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:05 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration