Skip to content

Instantly share code, notes, and snippets.

@YumaInaura
Last active September 2, 2018 00:52
Show Gist options
  • Save YumaInaura/7abb59592c28b5293620b4a733e2ef1a to your computer and use it in GitHub Desktop.
Save YumaInaura/7abb59592c28b5293620b4a733e2ef1a to your computer and use it in GitHub Desktop.
Perl — STDIN and pattern matching oner liner

Perl — STDIN and pattern matching oner liner

Use this syntax.

<> =~ /[PATTERN]/

Example. Simple word match

echo "Alice" | perl -e 'print "Here is Alice\n" if ( <> =~ /Alice/ )'
Here is Alice

Example. Regex match

echo "Alice" | perl -e 'print "Here is Alice\n" if ( <> =~ /^Al.ce$/ )'
Here is Alice

Example. Regex capture

echo "Alice" | perl -e 'print "Here is Ali${1}${2}\n" if ( <> =~ /Ali(.)(.)/ );'
Here is Alice
echo "Alien" | perl -e 'print "Here is Ali${1}${2}\n" if ( <> =~ /Ali(.)(.)/ );'
Here is Alien

NOTE

  • <> means STDIN
  • -e option for one liner

Ref

Version

  • bash 4.2
  • perl v5.28.0

Links

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment