Gossamer Forum
Home : General : Perl Programming :

Re: [petermcdonald] Setting specific line in file as variable

Quote Reply
Re: [petermcdonald] Setting specific line in file as variable In reply to
Quote:
Actually, come to think of it, I do need to "replace that line" as well. Any idea how I might do that?

Code:
my $var = 'Test';
my $new = 'Hello my friend!';
my $fa = 'test.txt';
my $fb = 'test.tmp.txt';

open FHA, "<$fa" or die $!;
open FHB, ">$fb" or die $!;
while (<FHA>) {
if (/^\Q$var\E$/i) {
print FHB "$new\n";
next;
}
print FHB;
}
close FHB;
close FHA;

unlink($fa) or die $!;
rename($fb, $fa) or die $!;

Quote:
Also, another problem (similar, but for a different script I'm making)... I want to read an HTML file, and set everything AFTER the tag <!--EDITBEGIN--> and BEFORE the tag <!--EDITEND--> as an variable.

Code:
my $file = '/path/to/file.html';

open FH, $file or die $!;
my $string = do { local $/; <FH> };
close FH;

my ($wanted) = $string =~ m|<!--EDITBEGIN-->(.+?)<!--EDITEND-->|si;

Last edited by:

Paul: Sep 2, 2002, 11:35 AM
Subject Author Views Date
Thread Setting specific line in file as variable petermcdonald 6410 Sep 1, 2002, 4:21 PM
Thread Re: [petermcdonald] Setting specific line in file as variable
durp 6282 Sep 1, 2002, 10:08 PM
Thread Re: [durp] Setting specific line in file as variable
petermcdonald 6290 Sep 1, 2002, 10:50 PM
Thread Re: [petermcdonald] Setting specific line in file as variable
petermcdonald 6282 Sep 1, 2002, 11:06 PM
Thread Re: [petermcdonald] Setting specific line in file as variable
Paul 6266 Sep 2, 2002, 2:10 AM
Post Re: [Paul] Setting specific line in file as variable
durp 6260 Sep 2, 2002, 10:31 AM
Thread Re: [Paul] Setting specific line in file as variable
petermcdonald 6251 Sep 2, 2002, 10:34 AM
Thread Re: [petermcdonald] Setting specific line in file as variable
petermcdonald 6274 Sep 2, 2002, 10:37 AM
Post Re: [petermcdonald] Setting specific line in file as variable
Paul 6266 Sep 2, 2002, 10:50 AM
Thread Re: [petermcdonald] Setting specific line in file as variable
Paul 6227 Sep 2, 2002, 10:56 AM
Thread Re: [Paul] Setting specific line in file as variable
petermcdonald 6245 Sep 2, 2002, 11:07 AM
Thread Re: [petermcdonald] Setting specific line in file as variable
Paul 6244 Sep 2, 2002, 11:34 AM
Thread Re: [Paul] Setting specific line in file as variable
petermcdonald 6257 Sep 2, 2002, 11:55 AM
Thread Re: [petermcdonald] Setting specific line in file as variable
Paul 6250 Sep 5, 2002, 4:21 AM
Post Re: [Paul] Setting specific line in file as variable
petermcdonald 6208 Sep 7, 2002, 6:07 PM