ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am trying to write a small CGI program to edit one line of a very long aliases file. The one line consists of the name: pageoncall and a pager number in the form of: 1234567890@pager.com. The pager number listed within that aliases could be in several other locations within the file. I have written most everything with the exception of this. I cannot figure out how to edit just that one line within the file. Here is the code that i have so far:
Code:
#!/usr/bin/perl
use CGI qw(:standard);
open(FILE, "/test/aliases") or die "Couldn't open $0";
print header('text/html'),
start_html('Answer the call!'),
h1('Select your pager number and click submit:'),
start_form,
popup_menu(-name=>'pager',
-values=>['','1234567890@messaging.sprintpcs.com',
'1234567890@archwireless.net',
'0987654321@archwireless.net']),p,
submit,
end_form;
if (param() )
{
my $pager = param('pager');
if($pager == '')
{
print "You selected an invalid number, please try again.";
}
while(<FILE>)
{
if(/^pageoncall:/)
{
print FILE "pageoncall: $pager";
}
}
}
If the file is not HUGE, I like to do this sort of thing by reading the entire file into an array, and then iterating over the array, performing modifications/edits on each iteration. Finally, write the array back out to the original file, or a new file.
(untested)
Code:
open( FILE, "$filename" ) || die "No can do... $!\n";
my @fileData = <FILE>;
close FILE;
foreach my $line ( @fileData ){
if( $line =~ m/^pageoncall:/ ){
# Modify $line according to your requirements here...
}
}
open( FILE, ">$filename" ) || die "No can do... $!\n";
print FILE @fileData;
close FILE;
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.