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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

splitting a line

Status
Not open for further replies.

tztexas

Technical User
Mar 10, 2005
8
US
Hi,
I've exported my Windows Services to a txt file. The header is
Name Description Status Startup Type Log On As

which is delimitted by tabs.

I only want the "name" and the "status" (for the entire services in my txt file). I was trying to get the "name", then try to figure out how to tab over and split the line to get the "status" but I get the entire line.

#!/usr/bin/perl

open (filein, "services.txt");

while ($line1=<filein>)
{

if ($line1 =~ /\t/)
{
($trash,$line) = split("/\t/",$line1);
# print "\n",$line1,"\n";
}
}

Thanks

 
A couple suggestions first: always use the strict and warnings pragmas (the use strict; and use warnings; lines) you'll catch a lots of errors that way.

Also, standard practice is usually to name your filehandles in upper case. It's not required, of course, but what can it hurt?

Code:
use strict;
use warnings;

open FILEIN, "< services.txt";

my $line1;
while ($line1 = <FILEIN>) {
   my @temp = split(/\t/, $line1);
   my ($name, $status) = @temp[0,2];
   print "Service: $name - Status: $status\n";
}

close FILEIN;
 
Hi rharsh,

A question that regarding the line "use warnings;".
I use SSH to remote access some Unix service to run my perl script, if I including "use warnings;" line in my perl script, it always give me such error message

Can't locate warnings.pm in @INC (@INC contains: /db4/usr/local/ /raid1/users/natasha/modules /usr/local/perl/Modules /usr/lib/perl-5.005/lib/5.00503/alpha-dec_osf /usr/lib/perl-5.005/lib/5.00503 /usr/lib/perl-5.005/lib/site_perl/5.005/alpha-dec_osf /usr/lib/perl-5.005/lib/site_perl/5.005 .) at batch_run_spidey.pl line 6.
BEGIN failed--compilation aborted at batch_run_spidey.pl line 6.

Any suggestion? Thanks in advance.
 
does this happens with "use whatever" ?
or just with "use warnings"?

TIMTOWTDI
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top