help with regular expressions

J

Jay

Hello,

just getting grips with Perl and RE, but need your help.

I am trying to open a file, print its contents to a textbox, but
extract certain bits of information before outputing them to the
textbox. LAter I want to save the contents back to the same file with
the bits added back to the file. All this done without the user even
realising. The bits I want to take out (and put back) are always
located at the beginning and end of each file.

This is what I have come up with so far:

# places a specified file into the textarea to be edited
sub get_file
{
open(FILE, "< $selected_file") or die "Cannot open file\n";
while ()($thisrow = <FILE>))
{
$thisrow =~ s/\<\/textarea\>/\<\*textarea\>/;
if ($thisrow !~ m/<^TMPL_INCLUDE/)
# "<TMPL_INCLUDE" is the string to be removed
{
print $thisrow;
}
}
close FILE;

This obviously doesn't work and I was wondering if any of you guys
could shed some light to the darkness that I am experiencing.

Thanks so much!!
 
T

Tad McClellan

Jay said:
while ()($thisrow = <FILE>))


What's with all those (unbalanced) parenthesis?

$thisrow =~ s/\<\/textarea\>/\<\*textarea\>/;
^ ^ ^ ^ ^
^ ^ ^ ^ ^

What's with all those unnecessary backslashes?


This obviously doesn't work and I was wondering if any of you guys
could shed some light to the darkness that I am experiencing.


You have a syntax error in the while statement.
 
J

j355

You description is a bit vague, but you could try this for starters:

perl -i -ne 's/foo/bar/;print if !/whatever/;' myfilename.dat
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top