Page is a not externally linkable
- Code, Content, and Presentation
-- Perl Server Side CGI Scripting
---- Perl script to return a specific line of a file?


mathis - 9:31 pm on Jan 20, 2004 (gmt 0)


Hey guys, you finally made it! I would be lost without your help. I thought it would be nice to post the complete thing if someone stumbles on this page later...

phonebook.txt has the following format:
012345824=Given Name Lastname
04385639=Name2 Whatever
...

and number2name.pl looks like this:
[perl]
#!/usr/bin/perl -w
#
# this pgm looks for argument ($ARGV[0]) in file $infile

$infile = "phonebook.txt";
$pattern = $ARGV[0]; # some more checking of argument should be done in here
$found = 0;

open(INPUT, $infile) ¦¦ die "Error opening $infile : $!\n";

while(<INPUT>) {
if (/$pattern/) {
s/.*?=//;
print;
$found += 1;
exit; # put the exit here, if only the first match is required
}
}
if ($found == 0) {
print $pattern . "\n";
}
close(INPUT);

__END__
[/perl]

my fli4l router/server does

echo %d %t =\> `./number2name.pl %m`: `./number2name.pl %p` >> phonecalls.log

where %d is date, %t is time, %m is the called number on my side (aka MSN) and %p is the calling phone number.
It works very fine with the tick-a-stat gnome panel applet which follows the log file...


Thread source:: http://www.webmasterworld.com/perl/3424.htm
Brought to you by WebmasterWorld: http://www.webmasterworld.com