next up previous contents
Next: Using the -s Option Up: Example uses of command-line Previous: Using the -n and

Using the -i Option

The -i option lets you modify files in-place. This means that Perl will automatically rename the input file and open the output file using the original name. You can force Perl to create a backup file by specifying a file extension for the backup file immediately after the -i. For example, -i.bak. If no extension is specified, no backup file will be kept.

One of the more popular uses for the -i option is to change sequences of characters. This kind of change normally requires 10 or more lines of code. However, using command-line options you can do it like this:

perl -p -i.bak -e "s/harry/tom/g;" test.dat

This command-line will change all occurrences of "harry" to "tom" in the test.dat file.



dave@cs.cf.ac.uk