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

Read Just One Line from A Text File

Status
Not open for further replies.

staticD

MIS
Sep 10, 2004
1
US
I'm Trying to find a way to read a file from a remote machine and log it into a .csv file, for a school I do some volunteering at.
The problem is that I only need the second line. The text file its reading looks like this.

[DefDates]
CurDefs=20041006.020
LastDefs=20040930.019

The first problem I am having is getting just the second line. The second problem is it dies at "for each $client"
Any help I could get on this would be greatly appreciated.




# Get Nav Defs
use warnings;
$LOGFILE = "\\\\$client\\c\$\\Program Files\\Common Files\\Symantec Shared\\VirusDefs\\definfo.dat";
my $filename = "report.csv";

$|++;
open F, ">$filename";

my @servers = qw(
caf1
caf3
1st1
1st2
kin1
kin4
2nd1

); # just a start

foreach my $client (@servers) {
open (LOGFILE) or die("***FLAMING DEATH***");
foreach $line (<LOGFILE>) {
chomp($line);
print "$line\n";
print F "$line, $client\n";
}
}
close


 
staticD,

Some of your code isn't quite valid, and some parts might be valid but I personally haven't seen the syntax before ;). To open a file for reading:

open(FILEHANDLE, "< C:/location/of/file.txt");

If you want a file for writing, change the "<" to a ">".

From here, you can do a lot of nifty things. If you are only interested in getting the second line of this file, try:

my $line = <FILEHANDLE>; # First line
$line = <FILEHANDLE>; # Second line

# Do stuff with $line

As for your second problem, you'll have to be a bit more specific. What is the compiler's error message? The line directly after that one looks a bit iffy; open (LOGFILE) isn't a way to open a file, as far as I know.

HTH,

Nick
 
$LOGFILE is a string containing (presumably) the name of the file you want to open. You need to open $LOGFILE to a file handle. Note that $LOGILE must be defined inside your "$client" loop so that the correct value of $client interpolates into the string. (Not tested.)
Code:
foreach my $client (@servers) {
    [b]my $LOGFILE = "\\\\$client\\c\$\\Program Files\\Common Files\\Symantec Shared\\VirusDefs\\definfo.dat";
    open (FH, $LOGFILE) or die("***FLAMING DEATH***");
    <FH>; #read and discard first line
    my $line = <FH>; #second line[/b]
    chomp($line);              
    print "$line\n";
    print F "$line, $client\n";
    [b]close(FH) || die qq(Can't close "$LOGFILE" on "$client"!\n);[/b]
}
HTH




 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top