Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retrive command line output into Perl (with spaces between words)

Status
Not open for further replies.

NavMen

Vendor
Jul 6, 2004
35
NL
Hi,

I try to create a perl script which execute a command and to receive the information (output) from a the command into variables of perl and to store them in a database.

Works fine! Except when there is a space in the output, like name:”VSS Data” this generate a problem in storing the information into the database.

Is there a way to tell Perl that the first 20 charaters of the ouput belongs to the first variable ($sscreate1) and the the next 20 by $sscreate2 etc.

This is the script I created:

#!C:/perl/bin/perl.exe

use Win32::ODBC;


$db = new Win32::ODBC("TestDB");

open (MM, 'mminfo -s Server –r "sscreate(20),sscomp(20),volume(20),level,ssid,totalsize,group,client,name(64),nfiles, metric, type" -t "2 week ago" |') || die "Cannot get mminfo report\n"; @bsinfo = <MM>;

foreach (@bsinfo) { ($sscreate1, $sscreate2, $sscomp1, $sscomp2, $volume, $level, $ssid, $totalsize, $group, $client, $name, $nfiles, $metric, $type) = split;
if ($sscreate1 eq "sscreate1") { next; }

$db->Sql("INSERT INTO NW_SAVESETS (sscreate1, sscomp1, volume,level, ssid, totalsize, grp, client, name, nfiles, metric, type) VALUES ('$sscreate1 $sscreate2','$sscomp1 $sscomp2','$volume','$level', '$ssid', '$totalsize', '$group', '$client', '$name', '$nfiles', '$metric', '$type')");

print "$sscreate1"; print " $sscreate2"; print " $sscomp1"; print " $sscomp2"; print " $volume"; print " $level"; print " $ssid";print " $totalsize";print " $group";print " $client";print " $name";print " $nfiles";print " $metric";print " $type\n"
} # end of foreach

$db->Close();


All help is welcome
Thanks,

NM


 
Your problem is your split, it's spitting on whitespace by default

You'll need to come up with someway of using say "::" to join your fields, if you intend to use split.

substr is probably what you need here, so you can specify the offset and length of each field

HTH
--Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ... smack the fecker
 
Following on from PaulTEG's post, the mminfo command looks vaguely SQL-like. Does it have any facility for returning literals in between the arguments, like
Code:
–r "sscreate(20),'::',sscomp(20),...
for example?

This would make it easy enough to use split, without the hassle of substr.

Steve

 
Steve,

That is not possible this mminfo (the command) returns an error, what must I do to get Attrib to work in my Script. I’m not a Perl guru :-(

Thanks

NM
 
Attrib? That's the first time you've mentioned Attrib.

To cut up a string into fixed length pieces use substr(), like this:

$first = substr($_,0,20);
$second = substr($_,20,20);

and so on. The parameters to substr() as as follows

subsstr(string,start_point,length)

string is the string you're looking at
start_point is the starting point in the string - first character is character zero
length is the number of characters from start_point



Mike

"Deliver me from the bane of civilised life; teddy bear envy."

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top