Quick and Dirty Perl

One-Liners

Although Perl is mainly known as a programming language, it can be surprisingly powerful when simply running from the command line. The general format is:

perl <options> -e 'program' file1...
The main options are as follows:
-w
Print a warning if program does not have correct syntax.
-p
Read each line of every specified file and write it out again after program has perhaps changed the line. Basically, this option creates a loop like this:
for each line in each file {
  read line
  execute program
  write line
}
-n
Read and process every line, but do not write automatically. Something gets written only if program contains (usually ends with) a write command. The loop is like the preceding, but without the automatic “write” statement.
-l
(That’s an “el”.) Clip the line feed from the end of each line when read, and then add a line feed again at the end of every output line if needed.
-i
Overwrite the original file(s) instead of writing to the default, which is standard output. Use the form -i.bak (or any other characters following the -i) to create a backup with that extension.
-e
Execute what follows (enclosed in quotes) as a Perl program. The program can have more than one command, separated by semicolons. Each command is applied to every line read from the input file(s), and then each line is written (or not written) according to the -p or -n options. By its nature, -e and its program must come after all other options. You can have more than one -e 'code' section.

Examples

None of the examples so far actually needed the -l (line-end) option, since each line I printed already had a CR attached from the original reading of the line. It’s a good idea to put it in all the time, though, unless you are doing tricky printing where you want several things printed on the same line.

Handy Variables

$_
The default variable, used by Perl in most places where you don’t specify things by name. In the above examples, each line is read into $_, and the print command, without an argument, shrugs and prints the contents of $_. Similarly, the s/.../.../ substitution worked on $_ by default, possibly changing what would later be printed.
$.
The record number (line number) of the file(s) currently being read. It does not start over at 1 for each new file.
$&
The exact characters that matched the RE.
$`
(Dollar backquote) The portion of the line before the match.
$'
(Dollar quote) The portion of the line after the match. Note that $`$&$' thus reproduces the entire line.
$1, $2, ...
The characters matched inside the first, second, etc. set of parentheses.
$ARGV
The name of the file currently being processed.

More examples

Even though these examples are all variations on “search-and-print”, none of them could have been done by egrep, by the way.



Last modified: Tue Sep 11 20:12:14 CDT 2012