one-liners beginner

How to use perl -e to print lines containing a pattern?

Question

How to use perl -e to print lines containing a pattern?

Using perl -e to print lines matching a pattern typically involves the -n or -p switches, which wrap your code in a loop over input lines. However, if you want to run Perl code directly through perl - without command-line switches, you need to handle input explicitly in your script.

Why the previous one-liner failed

The error you encountered:

Bareword found where operator expected ... near "'print if /foo/' input"

means that the command-line was interpreted incorrectly because -ne and the code string plus input filename were given directly as Perl code (not as command-line arguments). The perl -e or perl -ne switches must be passed to the Perl interpreter directly in the shell, not within a script.

Correct approach for perl -e

  • Use perl -ne 'print if /pattern/' filename at the shell to search files or STDIN.
  • This is a command for the shell, not Perl source code to run inside perl -.

Printable example runnable via perl - without switches

To follow the requirement of a fully runnable Perl script through perl - (no switches), simulate input lines and explicitly loop:

use strict;
use warnings;

# Simulated input lines (replace or extend as needed)
my @lines = (
    "This is a foo line",
    "No match here",
    "Another foo example",
    "Nothing matches"
);

my $pattern = qr/foo/;

foreach my $line (@lines) {
    print "$line\n" if $line =~ $pattern;
}

Key points about Perl here

  • -n and -p are command-line switches that wrap your code in an input loop:
    • -n: while (<>) { ... }, no automatic print
    • -p: like -n but adds print for each line
  • Inside these loops, $_ is the default variable holding the current line.
  • When running a pure script with perl -, you must create your own loop over lines (from array, <>, etc.)
  • Regex matching uses =~ /pattern/ on a scalar string
  • Perl’s slogan “TMTOWTDI” (There’s more than one way to do it) means you can choose between switches for quick one-liners or full scripts as fits your context.

Common pitfalls

  • Trying to combine switches like -n inside code run by perl - won’t work—those are command-line arguments to the Perl interpreter, not Perl code.
  • For one-liners, enclose code in single quotes to avoid shell interpolation side effects.
  • Input redirection or filename arguments must be given outside the quotes.

In short, to print lines containing a pattern using perl -e, run it directly on the command line like this:

perl -ne 'print if /foo/' input.txt

For embedding this logic in a sandboxed or script file environment where you invoke perl - with inline code only, write explicit loops as shown above.

Verified Code

Executed in a sandbox to capture real output. • v5.34.1 • 8ms

Tip: edit code and use “Run (Browser)”. Server runs always execute the published, verified snippet.
STDOUT
This is a foo line
Another foo example
STDERR
(empty)

Was this helpful?

Related Questions