Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Printing to a specific place in a file

by lagrenouille (Acolyte)
on Oct 04, 2001 at 06:27 UTC ( [id://116626]=perlquestion: print w/replies, xml ) Need Help??

lagrenouille has asked for the wisdom of the Perl Monks concerning the following question:

G'day again

Still going on this news script I'm afraid...

Now it will print out the data I want it to... but not within the scope of the html document I'm trying to add it to eg.

</HTML> <TD>Post From: lagrenouille</TD> <TD><B>The Headline</B></TD> <TD>all the data i entered</TD>

Problem is its AFTER the HTML document

How can I make my script find a specific point in the document eithe rby line number or a maker I put in the HTML and still write out the data I want it to?

Thanks
La Grenouille

Replies are listed 'Best First'.
Re: Printing to a specific place in a file
by Fletch (Bishop) on Oct 04, 2001 at 06:38 UTC

    Check out perldoc -q 'insert a line' (the question in question is in perlfaq5, "How do I change one line in a file/delete a line in a file/insert a line in the middle of a file/append to the beginning of a file?" if the -q doesn't work).

Re: Printing to a specific place in a file
by kschwab (Vicar) on Oct 04, 2001 at 06:41 UTC
    Try :perldoc -q 'change one line'.

    The general idea is that you can't change bytes in the middle of a file unless you are replacing existing data with replacement data that is exactly the same length.

    Perl does have a concept of "in-place" editing that can make it appear like you are inserting bytes into the middle of a file, but it's actually rewriting the whole thing...see perldoc perlrun(specifically the -i switch), and perldoc perlvar (the $^I variable).

Re: Printing to a specific place in a file
by tachyon (Chancellor) on Oct 04, 2001 at 10:13 UTC

    Here is a little inplace edit script to play with (backup with .bak suffix written):

    #!/usr/bin/perl -i.bak -w @ARGV = ('c:/test.txt'); # set our file to process while (<>) { print "newline\n" if m/somecond/; # add a new line is line matches +somecond s/this/that/; # substitute this for that on eve +ry line print; # write the modified (or not) lin +es }
    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      You can make it even simpler:
      #!/usr/bin/perl -i.bak -wp c:/test.txt print "newline\n" if m/somecond/; s/this/that/;

      IMHO command line switches are a Good Thing (tm).

Re: Printing to a specific place in a file
by Anarion (Hermit) on Oct 04, 2001 at 13:22 UTC
    If you want to move forward or backward in the file,use seek, or if you know the line number skip all lines before $. is that line.

    $anarion=\$anarion;

    s==q^QBY_^=,$_^=$[x7,print

Re: Printing to a specific place in a file
by IOrdy (Friar) on Oct 04, 2001 at 17:19 UTC
    Put a tag in your .html file called something like
    {news} Read your whole file into a variable say $page_html and replace: $page_html =~ s/\{news\}/$news_html/;
    Simple enough.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (9)
As of 2024-04-18 12:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found