A basic overview of some perl one-liners.
Quick and dirty perl.
Date Created:Friday December 29th, 2006 03:41 AM
Date Modified:Wednesday July 30th, 2008 05:34 PM
print the third colomn of lines in noisey.vfl
perl -nae 'print $F[3],"\n";' noisey.vfl
$F[1] in perl is like $1 in awk
$F[$#F] in perl is like $NF in awk
@ARGV command-line arguments stored in this array
$ARGV[0] first arg
$ARGV[1] second arg
$#ARGV is the last argument in an array
$#ARGV + 1 is the number of arguments in an array
OPTIONS
-p loops over and prints input
-n loops over and does not print output
-l strips newlines on input, adds them to output...good for efficiency
-i in-place edit, can make backups and overwrite files
-e execute the following, this should always be last
-a turns on autosplit mode, which after reading each line of input, Perl will
automatically do a split on the line and assign the resulting array to the @F variable.
Each line is split up by whitespace unless -F parameter is used to specify a new field delimiter.
-F specifys a new field delimiter.
SHORTHAND CHARACTER CLASSES
. match any character but \n same as [^n]
\b whole words only
\w word characters
\W non-word character
\s whitespace character
\S non-whitespace character
\d digit character
\D non-digit character
\t tab
\n newline
\r return
SYMBOLS
=~ like ==
!~ negated =~
m match operator
^ beginning of the line $string =~ m/^name/
$ end of the line $string =~ m/name$/
i case insensitivity
\b is the word boundry. When string has a ".", you must keep these in mind:
lets say you want to match "facts."
doesn't work:
perl -ple 's@\bfacts.\b@header@i' news.php
works:
perl -ple 's@\bfacts\b.@header@i' news.php
\W will match all non-word characters, so this will also work (notice no ".", because thats what \W is doing)
perl -ple 's@\bfacts\b\W@header@i' news.php
matching </font> and demonstrating the use of spaces and characters for the second string
perl -ple 's@\W\Wfont\W\W@this string is much more free < / > ! @i' news.php
you can also comment out characters using \
perl -ple 's/\$/a variable!/g' hscript.txt
whitespace = [ \t] or \s
perl is fun
perl[ \t]is[ \t]fun
perl\sis\sfun
REPLACE
regular expression replace
perl -pe 's/title/header/g' proj.php
perl -pe 's/\btitle\b/header/g' proj.php
perl -ple 's@\btitle\b@header@g' proj.php
match title or body
perl -ple 's@\b(title|body)\b@header@g' proj.php
/i makes regular expression case insensitive
perl -ple 's@\b(title|body)\b@header@i' proj.php
using [] to match grey or gray
perl -ple 's@gr[ae]y@black@g proj.php
IN-PLACE EDIT
changes occurences of matched pattern, writes backup of original file appending .old
perl -p -i.old -e 's@\btitle\b@header@g' proj.php
changes the actual file, not making backup
perl -i -ple 's@\btitle\b@replaced@g' proj.php
perl -i -ple 's@\btitle\b@replaced@g' *.php
prints local time
perl -e 'print scalar localtime'
perl -e 'system(date)'
incements all numbers
perl -pe 's/(\d+)/ 1 + $1/ge' proj.php
prints #!/usr/bin/php in all files with .php
perl -i -ple 'print q{#!/usr/bin/php} if $.==1; close ARGV if eof' *.php
using groups in matching...this will return the lines with vowels
perl -ple 'if($string =~ m/(A|E|I|O|U|a|e|i|o|u|)/){print "$string"};' news.php
perl -ple 'if($string =~ m/^(A|E|I|O|U|a|e|i|o|u|)/){print "$string"};' news.php
TRANSLATIONS
changing caps
perl -ple 'tr/[A-Z]/[a-z]/' news.php
perl -ple 'tr/[a-z]/[A-Z]/' news.php
changing c to 6 and g to 7
perl -ple 'tr/[c,g]/[6,7]/' news.php
FUNCTIONS IN PERL--usefull functions-->
I/O functions:
open, print and close
Processes and system groups:
system
Filehandles, files, and directories:
chdir, chmod, chown, mkdir, rename, rmdir
Flow of program control:
die, do
OPEN, PRINT, CLOSE
while a line contains a "$" it will print that line, if the file does not exist th "$!" prints the error
perl -e 'open(INPUT, "< hscript.txt") or die "no file: $!\n"; while (<INPUT>) {print if /\$/;} close(INPUT);'
SYSTEM
perl -e 'system("cat noise.vfl")'
PRINT AND EVAL
perl -e 'print(eval(4*5+2))'
RENAME
perl -e 'rename "yo.txt", "funk.txt"; print $!'
CHDIR AND DIE
perl -e 'chdir("../" or die "$!\n;"); system(pwd)'
perl -e 'chdir("hip" or die "$!\n;"); system(pwd)'
LOOPING
perl -ple 'foreach $argnum(0 .. $#ARGV) {print "$ARGV[$argnum]";}' hscript.txt
Places hcommand 12000 in front of every line
perl -ple 's/^/hcommand 12000/g' ProjectIDEAS
Downloads:
Download: perl.txt 4 KB
Please login or Click Here to register for downloads
Basic Perl by Dan Lynch
is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
Based on a work at www.3daet.com
Permissions beyond the scope of this license may be available at http://www.3daet.com
