Gossamer Forum
Home : General : Perl Programming :

Setting specific line in file as variable

Quote Reply
Setting specific line in file as variable
So I have this file, lets call it data.txt. It has multiple lines of text.

I want the script to find a line beginning with the variable $goto (previously defined in the script, 6 characters in length), and set the ENTIRE line as the variable $line . Each line in data.txt will begin with 6 unique characters.



Thanks,

Peter
Quote Reply
Re: [petermcdonald] Setting specific line in file as variable In reply to
Hi Peter,

There's at least a couple of ways you can do that.

1) Do a 'foreach' loop, "next"ing any line that doesn't start with the $goto variable you set. Ex:
foreach $line(@lines) {
next unless ($line=~/^$goto/);
### do your stuff if it matches
last; # prevents further parsing of later lines, if you've already found the line you were looking for
}

OR

use the 'seek' method. I'm not real well-versed, because I only hacked it into an existing script some years ago. Search the perl pages for examples of 'seek'ing.

Hope this helps.
Quote Reply
Re: [durp] Setting specific line in file as variable In reply to
Thanks, I'll test it out.
Quote Reply
Re: [petermcdonald] Setting specific line in file as variable In reply to
Just tested it and it works great. Thanks!
Quote Reply
Re: [petermcdonald] Setting specific line in file as variable In reply to
Did you open/close the file twice to solve it?
Quote Reply
Re: [Paul] Setting specific line in file as variable In reply to
Paul asked "Did you open/close the file twice to solve it?"

I'm not sure if you were asking Peter, or asking me (the one who pasted the code for Peter to try), but I didn't open/close it twice (Peter may have, though.

I just found a potential "bug" in my code, though, so here's a redo (whole thing, so you'll see what I'm thinking):


# OPTION 1 (for when you're going to be checking the same file over and over again
open (FILE, "data.txt") || die "can't read from data.txt";
@lines=<FILE>;
close (FILE);
foreach $line(@lines) {
$line=$1 if ($line=~/^(.*)$/); # I like to untaint stuff .... 'specially when working with paths, but do it pretty much all the time just for good measure :-)
if ($line=~/^$goto/) {
# do your stuff, the line is already assigned to the variable '$line'
last;
}
}


# OPTION 2 (if you'll only be checking once for a match)
$line=''; # (predeclare the variable)
open (FILE, "data.txt") || die "can't read from data.txt";
while (<FILE>) {
if ($_=~/^$goto(.*)$/) { # untaint and match at the same time, leaving the variable '$line' null if no match found (ignores any lines that don't start with '$goto').
$line=$_;
# Do your stuff
last;
}
}
close (FILE);


Both ways, it only opens/closes data.txt once.
Quote Reply
Re: [Paul] Setting specific line in file as variable In reply to
No, I just needed to open it once. Here's my code:

open(DATFILE, "$path/$whatdir.txt") || die("Could not open file!");
@datfile=<DATFILE>;
close(DATFILE);

foreach $newline(@datfile) {
next unless ($newline=~/^$whatfile/);
($ecoursedesc,$ecoursenum,$ecoursename,$ecredits,$eregional,$egradreq)=split(/\|/,$newline);
last;
}



And that appears to do the trick :)



Peter
Quote Reply
Re: [petermcdonald] Setting specific line in file as variable In reply to
durp,

I'll try out the new code, but the existed one (seems to be) working just fine.



Peter
Quote Reply
Re: [petermcdonald] Setting specific line in file as variable In reply to
Ok sorry I must have misread your question as I thought you wanted to find the line and replace it with something else.
Quote Reply
Re: [petermcdonald] Setting specific line in file as variable In reply to
A better approach would be:

Code:
my $line;

open DATFILE, "$path/$whatdir.txt" or die "Could not open file: $!";
while (<DATFILE>) {
chomp;
if (/^\Q$goto\E$/) {
$line = $_;
last;
}
next;
}

That is less memory intensive.

Last edited by:

Paul: Sep 2, 2002, 10:57 AM
Quote Reply
Re: [Paul] Setting specific line in file as variable In reply to
Thanks Paul.

Actually, come to think of it, I do need to "replace that line" as well. Any idea how I might do that?

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. I would also like to be able to rewrite just this area of the page with another variable.

Thanks again. It's great having a forum like this when you're learning something new :)

Peter
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
Quote Reply
Re: [Paul] Setting specific line in file as variable In reply to
Thanks Paul. But I'm a bit confused about the second example. What variable is given to the content between the two tags? And what about writing something new in it's place?

Thanks,

Peter

P.S. Sorry for the questions, but I'm fairly new at programming.

Last edited by:

petermcdonald: Sep 2, 2002, 11:55 AM
Quote Reply
Re: [petermcdonald] Setting specific line in file as variable In reply to
>>
What variable is given to the content between the two tags?
<<

$wanted
Quote Reply
Re: [Paul] Setting specific line in file as variable In reply to
Thanks for the help.

I've managed to set it up so it reads the file, and sets everything between the two tags as a variable. But how can I write to the file just replacing what is between those two tags? (not the entire file)



Thanks,

Peter