Solved

system command output to array

Posted on 2004-04-30
20
6,452 Views
Last Modified: 2007-12-19
I would like to output the system command to an array.
below doesn't work, jinfo=0.


@jinfo=system("echo hello");
0
Comment
Question by:matgold
20 Comments
 
LVL 7

Expert Comment

by:fim32
ID: 10961071
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
 
LVL 84

Expert Comment

by:ozo
ID: 10963407
You don't need the split.
0
 
LVL 84

Expert Comment

by:ozo
ID: 10963413
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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Author Comment

by:matgold
ID: 10964614
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
 
LVL 84

Expert Comment

by:ozo
ID: 10964646
What is in $jinfo?
0
 
LVL 48

Expert Comment

by:Tintin
ID: 10973811
What does

command $file

return?
0
 

Author Comment

by:matgold
ID: 10977990
$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
 
LVL 84

Expert Comment

by:ozo
ID: 10979898
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
 

Author Comment

by:matgold
ID: 10981070
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
 
LVL 84

Expert Comment

by:ozo
ID: 10981261
#Now it sounds like you want something like
  $jout = join'',grep/insert|machine|date_con|days_of_week/i,`command $file`;
 
0
 

Author Comment

by:matgold
ID: 10982015
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
 
LVL 84

Accepted Solution

by:
ozo earned 25 total points
ID: 10982122
@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
 

Author Comment

by:matgold
ID: 10987276
thanks
0
 

Author Comment

by:matgold
ID: 10987419
one more thing,
where it says "[1]", if needed, can it be [1..10]
0
 
LVL 84

Expert Comment

by:ozo
ID: 10988181
$jname = join' ',(split/\s+/,join'',grep/insert/i,@jout)[1..10];
0
 

Author Comment

by:matgold
ID: 10988368
cool, thanks
0
 

Author Comment

by:matgold
ID: 11069818
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
 
LVL 84

Expert Comment

by:ozo
ID: 11072789
$jname = (split/\s+/,join'',grep/insert/i,@jout)[3] =~ s/-----*/none none/;
0
 
LVL 84

Expert Comment

by:ozo
ID: 11072840
Is this what you want?

 ($jname = (split/\s+/,join'',grep/insert/i,@jout)[3])=~ s/-----*/none none/;
0
 

Author Comment

by:matgold
ID: 11089366
yes, thank you
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Perl output collect 7 67
Excel to CSV conversion with specific columns 5 87
Put query string from URL request -  PERL HTTP Daemon 2 87
Perl Versus AWK? 7 52
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question