Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Adding <pre> at beginning </pre> at file end

by vinoth.ree (Monsignor)
on Apr 07, 2009 at 13:53 UTC ( [id://756027]=perlquestion: print w/replies, xml ) Need Help??

vinoth.ree has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Here I have a file like following,

Testing testset
reeeeeeeeeeeeeeeeeee
eeeeeeeeeeeeeeeeeeeeeeee
rrrrreeeeeeeeeeeeeee
eeeeeeeeeeeeeeeeeeeee

I need to insert html pre tag at beginning of the first line and html pre close at the end of the last line So, finally I should get the file like following

pre at beginning
Testing testset
reeeeeeeeeeeeeeeeeee
eeeeeeeeeeeeeeeeeeeeeeee
rrrrreeeeeeeeeeeeeee
eeeeeeeeeeeeeeeeeeeee
close pre tag at end of the file

How can I do it with regular expression ?

  • Comment on Adding <pre> at beginning </pre> at file end

Replies are listed 'Best First'.
Re: Adding <pre> at beginning </pre> at file end
by JavaFan (Canon) on Apr 07, 2009 at 16:03 UTC
    Regular expressions? Why bother?
    perl -pe 'BEGIN {print "<pre>"} END {print "</pre>"}' file.old > file. +new
Re: Adding <pre> at beginning </pre> at file end
by mickeyn (Priest) on Apr 07, 2009 at 14:17 UTC
    Hi,

    Not using a RE like you asked, but you can try something like:

    use strict; use Tie::File; my $file = shift; tie my @array, 'Tie::File', $file or die "can't open $file...\n"; unshift @array => '<pre>'; push @array => '</pre>'; untie @array;
    HTH,
    Mickey
Re: Adding <pre> at beginning </pre> at file end
by kennethk (Abbot) on Apr 07, 2009 at 14:23 UTC

    This is actually not a good time for regular expressions, though you could certainly use them. Assuming the entire text is in a single string, might I recommend the concatenation operator?

    If you really want to use a regular expression, a substitution using the $ and ^ metacharacters as well as the s modifier will work. Give perlre a read through.

      I agree with kennethk, just do
      $filecontent = "<pre>".$filecontent."</pre>";
      from your question I presume that you are talking about adding one line at the start of the file and one line at the end of file, then the above solution should work. (take care of writing $filecontent back to the file).
      If the file size is huge, as per the above solution you are slurping the entire file, so be careful and choose another efficient way :)

      Vivek
      -- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.
Re: Adding <pre> at beginning </pre> at file end
by moritz (Cardinal) on Apr 07, 2009 at 14:20 UTC
    You can use two substitutions, one anchored to the start of the string, one anchored to the end of the string. See perlretut for a gentle introduction to regular expressions - it's not all that hard to come up with the solution yourself.
Re: Adding <pre> at beginning </pre> at file end
by mr_mischief (Monsignor) on Apr 07, 2009 at 14:25 UTC
    You can do that with a regular expression, but there's an easier way. You can just open your new file, write the <pre> tag, write your current file's contents, then write the </pre> tag.

    my $input = 'existing.txt'; my $output = 'new.txt'; open ( my $in, '<', $input ) or die "Cannot open file $input for readi +ng: $!\n"; open ( my $out, '>', $output ) or die "Cannot open file $output for wr +iting: $!\n"; print $out "<pre>\n"; while ( <$in> ) { print $out $_; } print $out "\n</pre>\n"; close $out or warn "Error writing $output: $!\n"; close $in;

    Compare the above to either keeping track of which line you're on from the input or slurping the input file into memory and doing a regex substitution over the whole thing. I think the KISS principle applies here.
Re: Adding <pre> at beginning </pre> at file end
by lakshmananindia (Chaplain) on Apr 08, 2009 at 04:25 UTC

    Here is another way

    use strict; use warnings; open (FILE,"+<","test"); local $/=undef; my $a=<FILE>; $a =~ s/^(.*)$/<pre>$1<\/pre>/gs; seek(FILE,0,0); print FILE $a;

    But you have to be careful when the file size is huge

    --Lakshmanan G.

    The great pleasure in my life is doing what people say you cannot do.


Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://756027]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-25 21:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found