This page is not available in every language. Missing translations are disabled in the language selector.

Quick reference

Perl One-Liners (perl -pie) Cheatsheet

A quick reference guide for Perl in-place stream editing with -pie: search and replace, regular expressions, capture groups, and multi-file processing.

· Published

The perl -pie flags transform Perl into a powerful multi-file streaming search-and-replace editor.

Command Flag Meanings

perl -p -i -e 'code' file.txt
  • -p: Loop over input lines and print each line automatically (like sed).
  • -i: Edit files in-place (overwriting original file). Use -i.bak to preserve backup copies.
  • -e: Execute one line of Perl code.

Search & Replace Examples

Basic Global Substitution

# Replace 'hello' with 'world' across all .txt files
perl -p -i -e 's/hello/world/g' *.txt

With Backup Files

# Modifies original and saves original copy as file.txt.bak
perl -p -i.bak -e 's/foo/bar/g' file.txt

Regular Expressions & Backreferences

# Transform '@include align-items(center);' into 'align-items: center;'
perl -p -i -e 's/@include (align-items)\((.*)\);/$1: $2;/g' style.css

References

Related topics