How can I send the output of the system() command to a file and capture a string in that file

P

prashant_ec2003

Hello All,

I am newbie .
waiting for your suggstions and guidance.

My requirement : I need to send the output of the system() command to a
file , then extract a string from one of the lines in that file .How
can i do this.

Thanks in advance
Prashant
 
A

A. Sinan Unur

(e-mail address removed) wrote in @z14g2000cwz.googlegroups.com:
I am newbie .
waiting for your suggstions and guidance.

Instead of waiting, you should utilize your time by reading the posting
guidelines for this group to learn how you can help yourself, and help
others help you.
My requirement : I need to send the output of the system() command
to a

perldoc -f system
file , then extract a string from one of the lines in that file .How
can i do this.

perldoc perlretut

Sinan
 
J

Jürgen Exner

Hello All,

I am newbie .
waiting for your suggstions and guidance.

My requirement : I need to send the output of the system() command to
a file ,

Well, strictly speaking system() itself doesn't generate any output. To send
the output of the external command to a file just use the output redirect
command of your command shell to redirect the output to a file. Which symbol
that is depends on the command shell you are using, but very often a ">"
will work.
then extract a string from one of the lines in that file .How
can i do this.

You open() the file, read it line by line, and when you found the right line
exctact the string. Sorry, more detailed advice is not possible because you
didn't give us any details, either.

However, using system(), redirect the command output to a file, then reading
and scanning the file for a string is normally the wrong way to go.
Unless you need the external file for other reasons you will be way better
of using backticks or qx// to call the external program because those
commands will capture the command output and return it (details see "perldoc
perlop, section "strings"). Then all you have to do is to grep() the correct
line from the returned array and extract the string.

jue
 
P

prashant_ec2003

can i include the system() command in the open() commnad

example open ( FH , "system ("...") | ) and the like . it may be
incorrect but correct me if i am wrong..with the symbols used ..

This is the where i am finding difficulty .Please help
 
A

A. Sinan Unur

(e-mail address removed) wrote in @f14g2000cwb.googlegroups.com:
can i include the system() command in the open() commnad

example open ( FH , "system ("...") | )

You cannot do that. Have you read

perldoc -f system

yet?

system on returns information on whether the call succeeded, not the
output of the command executed.

You can, on the other hand, open a pipe to the program. For example:

#! /usr/bin/perl

use strict;
use warnings;

open my $ls_pipe, "-|", 'ls -l'
or die "Cannot open pipe to read ls output: $!";

print "Read: $_" while <$ls_pipe>;

close $ls_pipe
or die "Error closing pipe to ls: $!";

__END__
it may be incorrect but correct me if i am wrong ...

You did not make a statement, so you cannot be wrong.
with the symbols used ..

I don't know what that means.

Sinan
 
B

Big and Blue

can i include the system() command in the open() commnad

example open ( FH , "system ("...") | ) and the like . it may be
incorrect but correct me if i am wrong..with the symbols used ..

Try reading the documentation for "open".

Also, you say you need to write a file, then get Perl to read it. Is
that the actually requirement? Do you just in fact want Perl to be able to
extract a value from the output of an external command? And you assume
that using a file for this is the way to go?

If that is the case thne your assumptions are wrong (and try not to
post assumptions as requirements).

You can "open" an external command and read its outpur directly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,444
Messages
2,571,709
Members
48,796
Latest member
Greg L.
Top