• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6462
  • Last Modified:

system command output to array

I would like to output the system command to an array.
below doesn't work, jinfo=0.


@jinfo=system("echo hello");
0
matgold
Asked:
matgold
1 Solution
 
fim32Commented:
the system output usually ends up as a big string with line breaks included.  to put that to an array, just split it:

@jinfo=split "\n",`echo hello`;
0
 
ozoCommented:
You don't need the split.
0
 
ozoCommented:
perldoc -q "Why can't I get the output of a command with system()?"
Found in /Library/Perl/pods/perlfaq8.pod
       Why can't I get the output of a command with system()?

               You're confusing the purpose of system() and backticks (``).
               system() runs a command and returns exit status information (as
               a 16 bit value: the low 7 bits are the signal the process died
               from, if any, and the high 8 bits are the actual exit value).
               Backticks (``) run a command and return what it sent to STDOUT.

                   $exit_status   = system("mail-users");
                   $output_string = `ls`;
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
matgoldAuthor Commented:
open(filein, "jobs.lis");
@fileinput = <filein>;
close(filein);
foreach $file (@fileinput) {
  $jinfo=`command $file`;
  $jout=`grep -i prefix $jinfo`;
}

the grep is not working.
0
 
ozoCommented:
What is in $jinfo?
0
 
TintinCommented:
What does

command $file

return?
0
 
matgoldAuthor Commented:
$file = fid_hec
$jinfo =
insert_job: fidp_hec   job_type: c
machine: hostname
owner: user@host
permission: gx,wx,mx
date_conditions: 1
days_of_week: mo
start_times: "08:26"
description: "Prod hec"
0
 
ozoCommented:
That's a strange thing to pass to grep
What did you want grep to do with it?
Did you mean to say `grep -i prefix $file`?
0
 
matgoldAuthor Commented:
No !
$file = fid_hec, then I run a program against $file, to produce output to $jinfo.
since $jinfo have unwanted data, that's why I used grep.
I actually wanted to do "egrep -i insert|machine|date_con|days_of_week"

the final output that I'm looking for, should look like so (comma delimited).
fidp_hec,hostname,1,mo,Prod hec
0
 
ozoCommented:
#Now it sounds like you want something like
  $jout = join'',grep/insert|machine|date_con|days_of_week/i,`command $file`;
 
0
 
matgoldAuthor Commented:
great I'm half way there.
I'm planning on do the following.
$jname = join'',grep/insert/i,`autorep -q -J $file`;
$jmach = join'',grep/machine/i,`autorep -q -J $file`;
$jdate = join'',grep/date_conditions/i,`autorep -q -J $file`;
$jdow = join'',grep/days_of_week/i,`autorep -q -J $file`;

results:
$jname = "insert_job: fidp_hec   job_type: c "
$jmach = "machine: hostname"
$jdate = "date_conditions: 1"
$jdow = "days_of_week: all"

I know in unix, you can print by field number, like print $2.
I can't remember how it work in perl.
for example, on $jname, I want to print just "fidp_hec".
0
 
ozoCommented:
@jout = `autorep -q -J $file`;
$jname = (split/\s+/,join'',grep/insert/i,@jout)[1];
$jmach = join'',grep/machine/i,@jout;
$jdate = join'',grep/date_conditions/i,@jout;
$jdow = join'',grep/days_of_week/i,@jout;
0
 
matgoldAuthor Commented:
thanks
0
 
matgoldAuthor Commented:
one more thing,
where it says "[1]", if needed, can it be [1..10]
0
 
ozoCommented:
$jname = join' ',(split/\s+/,join'',grep/insert/i,@jout)[1..10];
0
 
matgoldAuthor Commented:
cool, thanks
0
 
matgoldAuthor Commented:
some where in @jout contain "-----", I would like to substitute it with "none none"
what can I do.

$jname = (split/\s+/,join'',s/-----*/none none/,grep/insert/i,@jout)[3];
0
 
ozoCommented:
$jname = (split/\s+/,join'',grep/insert/i,@jout)[3] =~ s/-----*/none none/;
0
 
ozoCommented:
Is this what you want?

 ($jname = (split/\s+/,join'',grep/insert/i,@jout)[3])=~ s/-----*/none none/;
0
 
matgoldAuthor Commented:
yes, thank you
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now