Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

How to use fseek in perl ??

by pysome (Scribe)
on Oct 24, 2007 at 11:27 UTC ( [id://646871]=perlquestion: print w/replies, xml ) Need Help??

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

Hi All,
I wanna add some text to a file in its fixed position.Say the line 3. How to achieve? That is , if the file(a.txt) like:
ab
cd
ef
ghij
klmn
I wanna add "a test content\n" after the "ef".My ideal result is :
ab
cd
ef
a test content
<blank line>
ghij
klmn

Replies are listed 'Best First'.
Re: How to use fseek in perl ??
by Somni (Friar) on Oct 24, 2007 at 12:29 UTC
    Perl 5.6 had an excellent FAQ entry on this very concept that quite often helps to clear up this natural confusion. You can find the FAQ entry here.

    The short answer is, use Tie::File.

      yeah. Tie::File is good choice.Thanks
Re: How to use fseek in perl ??
by mwah (Hermit) on Oct 24, 2007 at 11:53 UTC

    Your request would be solved formally by

    for Unix: perl -i.bak -0777 -lpe 's/\nef\n/\nef\na test content\n<blank line>\n +/' a.txt for Windows: perl -i.bak -0777 -lpe "s/\nef\n/\nef\na test content\n<blank line>\n +/" a.txt

    Addendum: If you only want to insert sth. after a specified line, then you may get along by doing this:

    Unix: perl -i.bak -pe 'print "a test content\n<blank line>\n" if $.==4' a.t +xt Windows: perl -i.bak -pe "print qq{a test content\n<blank line>\n} if $.==4" a +.txt

    So here we count on $. (line number) and the line after #3 is #4.

    Maybe you meant something else by your term "fseek in perl". Better you'd give a small hint what exactly you are trying to solve by that?

    Regards

    mwa

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-06-16 11:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.