Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^2: what is the best method to read and write in a file in perl

by superfrink (Curate)
on Sep 17, 2010 at 16:52 UTC ( #860526=note: print w/ replies, xml ) Need Help??


in reply to Re: what is the best method to read and write in a file in perl
in thread what is the best method to read and write in a file in perl

Here is an example using -i. Notice that a new file is created with the same name. This isn't quite the same as editing the same file. You could have problems if there are multiple hard links to the file you are editing.

[chad@desktop ~]$ ls -li asdf 2507085 -rw------- 1 chad chad 10 2010-09-17 10:44 asdf [chad@desktop ~]$ cat asdf 1 2 3 4 5 [chad@desktop ~]$ perl -p -i.bak -e 's/^2$/two/' asdf [chad@desktop ~]$ cat asdf 1 two 3 4 5 [chad@desktop ~]$ cat asdf.bak 1 2 3 4 5 [chad@desktop ~]$ ls -li asdf asdf.bak 2507203 -rw------- 1 chad chad 12 2010-09-17 10:46 asdf 2507085 -rw------- 1 chad chad 10 2010-09-17 10:44 asdf.bak [chad@desktop ~]$ perl -p -i -e 's/^2$/two/' asdf [chad@desktop ~]$ ls -li asdf asdf.bak 2507214 -rw------- 1 chad chad 12 2010-09-17 10:49 asdf 2507085 -rw------- 1 chad chad 10 2010-09-17 10:44 asdf.bak [chad@desktop ~]$
Update: the sed command also has a -i flag.


Comment on Re^2: what is the best method to read and write in a file in perl
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2014-07-13 18:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (251 votes), past polls