Perl Inplace Editing HOWTO

5 PM June 8, 2003

Since this is the third time in the last year I've had to look this up, I am making a note on my blog, where it should be easy for me to find :)

From this 'notes on Perl' page, how to inplace edit a file with perl:

  perl -p -i.bak -e "s#a#b#" filename

-pExecute the command(s) for every line.
-i.bakEdit the given file in-place ".bak" is the extension name of the backup file.
-eIntroduces the command to execute. The command in this case is 's' for substitute.

See also for information on regular expression syntax and such.

By alang | # | Comments (5)
(Posted to Software Development)


At 01:19, 19 Jul 2004 Richard Atkins wrote:

And if you happen to have access to a regular unix tool chain, you can issue much the same command with sed:

sed -i.bak -e 's/a/b/' <file>

I suppose you could even alias sed to be 'perl -p' if you were feeling adventurous.

You can of course substitute your own favourite characters for '/' (or character pair, such as [], {} and <> if you're using perl).

Note that if your doing a command line search and replace, you probably want to use the g option on your regexp to make it a global search, rather than just the first.

At 21:13, 28 Jul 2004 Ben Chapman wrote:

Well, it depends which version of sed you have. With version 3.x (RedHat 2.1 Enterprise among others) you don't get in-place editing. That's why the sed FAQ recommends perl for this task. See at 4.4.1, which has a couple of different solutions, including, for older sed:

 #! /bin/sh
 # filename: replaceall
 find . -type f -name '*.txt' -print | while read i
  sed 's|foo|bar|g' $i > $i.tmp && mv $i.tmp $i

This does show the use of pipe chars and a global replace.

At 19:42, 10 Sep 2004 david delikat wrote:

interesting bit, perl inplace editting only works witht he -p option, not the -n option, I would have thought the -p would produce output, but it appears that the printed lines go to the file being editted..

At 04:48, 17 Oct 2004 Shane Geiger wrote:

The *rpl* command is another alternative to perl and sed for editing a file.

rpl - intelligent recursive search/replace utility

One of the advantages of this program for new users is that it is more verbose than sed or perl about doing
these sorts of operations. (I have found this to be more welcoming for non-programmers.)

Quick Demo:

sgeiger@a31:~/demo$ echo "foo bar foobar" > temp.txt
sgeiger@a31:~/demo$ rpl -R -x'.txt' 'foo' 'bar' .
Replacing "foo" with "bar" (case sensitive) (partial words matched)
A Total of 2 matches replaced in 1 file searched.

Common Options:

rpl -Ribdvsptw -x'.txt' 'foo' 'bar' .
-- That uses these options: recursive, case-insensitive, backup, keep modification times, verbose,
simulate replacement, prompt, use TMPDIR, match only on word boundaries

At 21:34, 13 Apr 2005 anonymous wrote:

you can try to remember this by pie

perl -p -i -e


Add Comment

(Not displayed)

(Leave blank line between paragraphs. URLs converted to links. HTML stripped. Indented source code will be formatted with <pre> tags.)

© 2003-2006 Alan Green