Skip to content

Collection of useful perl one-liners to be used inside shell scripts

Notifications You must be signed in to change notification settings

aleksandarstojkovski/Useful-Perl-Oneliners

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 

Repository files navigation

Useful Perl Oneliners

Very usefull Perl one liners, designed to be used inside shell scripts.

First occurrence of regular expression

Command   :   perl -ne 'print "$1" and exit if /(REGEX_HERE)/'

Last occurrence of regular expression

Command   :   perl -ne '$e = $1 if /(REGEX_HERE)/;  END{ print $e }'

Print something if regex matches

Command   :   perl -ne 'print /(REGEX_HERE)/ ? "match" : "no-match" '
Example   :   echo "ABC123" | perl -ne 'print /ABC123/ ? "match" : "no-match" '  
Output    :   match

Print content of specific column (separated by spaces)

Command   :   perl -lane 'print $F[0]'
Example   :   echo "A B C" | perl -lane 'print $F[0]'
Output    :   A

Print content of specific column (separated by any character)

Command   :   perl -F[CHARACTER_HERE] -a -ne 'print "$F[0]" '
Example   :   echo "A;B;C" | perl -F; -a -ne 'print "$F[1]" '
Output    :   B

Print something if columns match specific regex

Command   :   perl -lane 'print "TRUE" if $F[0]=~/\d/ and $F[1]=~/\D/'
Example   :   echo "3 z" | perl -lane 'print "TRUE" if $F[0]=~/\d/ and $F[1]=~/\D/'
Output    :   TRUE

Sobstitute match in file

-pi.orig will create a backup of the file with ".orig" at the end

Command   :   perl -pi.orig -e 's/(REGEX_HERE)/(VALUE_HERE)/' (PATH_TO_FILE)

Check syntax of file (parse)

eg. in this case file format must be KEY;VALUE

Command   :   perl -ne '$exit_code='0' ; $exit_code='1' if $_ !~ /.*?\s;\s.*/ ; print "1" and exit if $exit_code' (PATH_TO_FILE)

Substitute multiple placeholders with one single one liner

File: example_config_file.txt (with placeholders)

<VirtualHost *:80>
    DocumentRoot ##DOCUMENT_ROOT##
    ServerName ##SERVER_NAME##
</VirtualHost>

File: script.sh

export DOCUMENT_ROOT="/var/www/"
export SERVER_NAME="example.com"
perl -pi.orig -w -e 's!##(.+?)##!$ENV{$1}!ge;' example_config_file.txt

Get the current time five minutes ago

POSIX format

perl -MPOSIX=strftime -le 'print strftime("%Y-%m-%d %H:%M:%S", localtime(time()-5*60))'

EPOC format

date -d "$(perl -MPOSIX=strftime -le 'print strftime("%Y-%m-%d %H:%M:%S", localtime(time()-5*60))')" +%s

About

Collection of useful perl one-liners to be used inside shell scripts

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages