Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: To add a string to the content of a file

by johnnywang (Priest)
on Nov 28, 2005 at 01:01 UTC ( [id://512071]=note: print w/replies, xml ) Need Help??


in reply to To add a string to the content of a file

or just do a one-liner:
perl -i.orig -p -e 's/^(#!.+)$/$1Add this line/' file

Replies are listed 'Best First'.
Re^2: To add a string to the content of a file
by sgifford (Prior) on Nov 28, 2005 at 04:36 UTC
    To expand a little bit, the -p flag tells perl to wrap the whole script inside a:
    whie (<>) { # ... your code print $_; }

    The -i.orig tells it to first rename the original input file to its old name followed by .orig, then run the script and put its output into a new file with the same name as the input file.

    I suspect the code snippet you started off with assumed you were running under perl -p.

Log In?
Username:
Password:

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

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

    No recent polls found