Fork me on GitHub

Project Notes

Patching Text Files

Simple tricks for updating text files with perl


Take theses three perl command options:

  • -i enables editing files in place, with the option to back up the original file first.
  • -p assume “while (<>) { print; }” loop around the program, so it works like sed
  • -e executes the program given on the command line

Together these allow simple one-liners for modifying text files with arbitrary complexity: from simple substitutions to conditional and sophisticated modifications.

The script has two examples to demonstrate the principle.

NB: on posix systems, use single quotes for -e programs to avoid shell command substitution.


Running the example:

$ ./
# Installing example.txt from template..

# Original file (example.txt):

# Example 1: General substitution (no file backup)
# changes all lines with 'line=*' to 'row=*'
# .. diff after change:
< row=1
< row=2
> line=1
> line=2
< row=3
< row=4
> line=3
> line=4

# Example 2: Dependent substitution (with backup)
# changes the right-hand-side of the line matching 'my_key='
# .. diff after change:

# Final example.txt after all these changes:

Operating on Multiple Files

Combine with find:

find . -name "example.txt" -exec perl -i -pe '
if ( eof ) {
  close ARGV;
' {} "my_new_key" \;

Note: to exclude paths from the find, say to exclude the lib folder, add a path with the prune option:

find . -path ./lib -prune -o -name "example.txt" ..

This reads as: find from here, prune ./lib paths or (-o) match name example.txt

Credits and References

About LCK#5 perl
Project Source on GitHub Return to the Project Catalog

This page is a web-friendly rendering of my project notes shared in the LittleCodingKata GitHub repository.

LittleCodingKata is my collection of programming exercises, research and code toys broadly spanning things that relate to programming and software development (languages, frameworks and tools).

These range from the trivial to the complex and serious. Many are inspired by existing work and I'll note credits and references where applicable. The focus is quite scattered, as I variously work on things new and important in the moment, or go back to revisit things from the past.

This is primarily a personal collection for my own edification and learning, but anyone who stumbles by is welcome to borrow, steal or reference the work here. And if you spot errors or issues I'd really appreciate some feedback - create an issue, send me an email or even send a pull-request.