CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
store grep value in variable so i can use that variable value

 



sandysk
New User

May 26, 2017, 2:13 AM

Post #1 of 4 (1901 views)
store grep value in variable so i can use that variable value Can't Post

Hi All,

I want to store "grep \'/123CHAR' %s | wc -l" output in one variable so I can use that variable later. I have written the below mention code. but I am not getting how to store value in variable.

sprintf(mytextFile,"\tmp\myfile.txt");
sprintf(cmd,"grep \'/123CHAR' %s | wc -l", mytextFile);
cmdreturn=system(cmd);


Laurent_R
Veteran / Moderator

May 26, 2017, 7:18 AM

Post #2 of 4 (1893 views)
Re: [sandysk] store grep value in variable so i can use that variable value [In reply to] Can't Post

Hi,
this does not look like Perl code.

If anything,

Code
grep \'/123CHAR' %s | wc -l

looks more or less as shell scripting under Unix or Linux.

sprintf does not print to a file, it formats a string, and it's first argument is a format string, for example something like "%s %2.7f".

Assuming cmd is supposed to contain a command issued to the system, it would need to be stored in a variable, for example $cmd, properly declared.

And system() returns the status return by the system command, not the output of that command. For getting the output use backticks `$cmd` or qx/.../.

It would be better if you explained, and illustrated with examples, what your input is supposed to and what you expect as output.


Laurent_R
Veteran / Moderator

May 26, 2017, 7:19 AM

Post #3 of 4 (1892 views)
Re: [sandysk] store grep value in variable so i can use that variable value [In reply to] Can't Post

And, with all due respect, please do not post in the intermediate section if you can't write a single valid Perl statement.


BillKSmith
Veteran

May 26, 2017, 7:42 AM

Post #4 of 4 (1892 views)
Re: [sandysk] store grep value in variable so i can use that variable value [In reply to] Can't Post

It is not clear what you are trying to do. My best guess is that you are trying to use sprintf to construct a shell command that will count the number of lines of a text file which contain a fixed pattern.

Why do you want to use the shell to do a task that perl does well? There is even a perl module File::Grep to make the task even easier.

To return to your original question, you are asking how to capture the output of shell command in perl. This is not possible with the function 'system'. The usual way to capture the output of a shell command is to use back-tics.

Code
my $number_of_matches = `$cmd`;


Of course the code you posted still will not work. It does not contain a single valid perl variable. I suggest that you read the perl document perlvar and the sprintf section of perlfunc. If you do not understand them, post questions in the beginners forum.
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives