Perl

= Resources = http://perlmeme.org/howtos/index.html

http://vsbabu.org/tutorials/perl/

= Perl shell = Creates a Perl shell to test Perl commands. my $count = 0; my $statement = ""; local $SIG{__WARN__} = sub {}; #suppress error reporting while (<>) { chomp; while (/{|\(|\[/g) {$count++};       while (/}|\)|\]/g) {$count--}; $statement .= $_; if (!$count) { eval $statement; if ($@) {print "Syntax error.\n"}; $statement = ""; $count = 0; } }
 * 1) !/usr/bin/perl -w
 * 2) from Perl Black Book
 * 3) type "exit" to exit

= Perl oneliner =

remove the empty line
perl -0777 -i -pe "s/\n\n/\n/g" filename
 * 1) remove the empty line

remove duplicate lines based on column value
perl -e ' $c=0; while(<>) { s/\r?\n//; @F=split /\t/, $_; if (! ($save{$F[$c]}++)) { print "$_\n"; } }  '   \ file1  >   outfile
 * 1) Removing lines where values are repeated in a column (first column $c=0 )

delete first 10 lines and make a copy of old
perl -i.old -ne 'print unless 1 .. 10' foo.txt
 * 1) delete first 10 lines and make a copy of old

just lines 15 to 17
perl -ne 'print if 15 .. 17' *.pod
 * 1) just lines 15 to 17

delete all but lines beween START and END
perl -i.old -ne 'print unless /^START$/ .. /^END$/' foo.txt
 * 1) delete all but lines beween START and END

HTML to text cenvertion
cat index.htm | perl -ne 's/\<[^<]*\>//g ;print $_'
 * 1) HTML to text cenvertion

Double space a file, except the blank lines.
perl -pe '$_ .= "\n" unless /^$/'
 * 1) Double space a file, except the blank lines.

Number all lines in a file.
perl -pe '$_ = "$. $_"'
 * 1) Number all lines in a file.

Number all lines, but print line numbers only for lines that match a pattern.
perl -pe '$_ = "$. $_" if /regex/'
 * 1) Number all lines, but print line numbers only for lines that match a pattern.

Print the sum of all the fields on a line.
perl -MList::Util=sum -alne 'print sum @F'
 * 1) Print the sum of all the fields on a line.

Print the sum of all the fields on all lines.
perl -MList::Util=sum -alne 'push @S,@F; END { print sum @S }'
 * 1) Print the sum of all the fields on all lines.

Replace each field with its absolute value
perl -alne 'print "@{[map { abs } @F]}"'
 * 1) Replace each field with its absolute value

-0777: treat file as a single line, this way we don't have to worry about ranges on our regex. -i: operate on file in place. super dangerous and fun. If you're a big wuss you could use -i.bak to automatically create a backup file. -n: use a while (<>) loop around your script. This is especially useful when combined with the use of the -e switch -pe: use a while(<>) loop and print out $_ after execution of code -V: print a summary of the perl configuation values use __LINE__ and __FILE__ to indicate the current file and current line number.

Merge 2 files based on a column - all rows from file1 and only common rows from file2
Merge 2 files based on a column value (e.g.: first column in file1 (c1=1) and file2 (c2=1)) : all rows from file1 and only common rows from file2  perl  -e ' $c1=1; $c2=1;  ($f1,$f2)=@ARGV;  open(F2,$f2);  while () { s/\r?\n//; @F=split /\t/, $_; $line2{$F[$c2-1]} .= "$_\n" };   open(F1,$f1);  while () { s/\r?\n//; @F=split /\t/, $_; $x = $line2{$F[$c1-1]};   if ($x) { $n = ($x =~ s/^/$_\t/gm); print $x; } else { print "$_\n";} } '   file1 file2 > file_merged

Merge 2 files based on a column value - only common rows from file1 and file2
Merge 2 files based on a column value (e.g.: first column in file1 (c1=1) and file2 (c2=1)) : only common rows from file1 and file2  perl -e ' $c1=1; $c2=1; ($f1,$f2)=@ARGV; open(F2,$f2); while () { s/\r?\n//; @F=split /\s/, $_; $line2{$F[$c2-1]} .= "$_\n" }; $count2 = $.; open(F1,$f1); while () { s/\r?\n//; @F=split /\s/, $_; $x = $line2{$F[$c1-1]}; if ($x) { $n = ($x =~ s/^/$_\t/gm); print $x; } } ' $1  $2  > $3

Save only uniq lines in file 1 or file 2 - not shared in both
Save lines in file 1 or file 2, but not in both. All lines that appear in first or second file, " but not in both ", will be saved  perl -e ' ($file1, $file2) = @ARGV; open F2, $file2 or die "$file2: $!\n"; @lines2 = ; foreach (@lines2) { $h2{$_}++; } open F1, $file1 or die "$file1: $!\n"; while () { if (exists $h2{$_}) { $h2{$_} = 0; } else { print $_;  } }  foreach (@lines2) { if ($h2{$_}) { print $_;  } } ; '  $1  $2  > $3

Save only lines in file 1 and not in file 2 - file1 minus file2
Save lines in file 1 but not in file 2.  perl -e ' ($file1, $file2) = @ARGV; open F2, $file2; while () { $h2{$_}++ }; open F1, $file1; while () { if (! $h2{$_}) { print $_; } }  ' $1  $2  > $3
 * 1) lines in file 1 but NOT in file 2

Transpose
 perl -e ' $_=<>; s/\r?\n//; @out_rows = split /\t/, $_; $num_out_rows = $#out_rows+1; while(<>) { s/\r?\n//; @F = split /\t/, $_; foreach $i (0 .. $#F) { $out_rows[$i] .= "\t$F[$i]"; }} END { foreach $row (@out_rows) { print "$row\n" } } ' file1 > file1_transposed.tab

Generate 10 random numbers between 5 and 15
 perl -le '$n=10; $min=5; $max=16; $, = " "; print map { int(rand($max-$min))+$min } 1..$n'
 * 1) Generate 10 random numbers between 5 and 15 (including both 5 and 15)

Convert all text to upper or lowercase
perl -nle 'print "\U$_"' perl -nle 'print "\L$_"'
 * 1) Uppercase
 * 1) lowercase

Invert the letter case
perl -ple 'y/A-Za-z/a-zA-Z/'

Strip leading whitespace
perl -ple 's/^[ \t]+//'
 * 1) Strip leading whitespace (spaces, tabs) from the beginning of each line

Strip trailing whitespace
perl -ple 's/[ \t]+$//'
 * 1) Strip trailing whitespace (space, tabs) from the end of each line

Strip whitespace from both sides
perl -ple 's/^[ \t]+|[ \t]+$//g'
 * 1) Strip whitespace from the beginning and end of each line

Convert newlines character in Unix/DOS/Mac
dos: \r\n unix: \n mac: \r

perl -p -e 's/\n/\r\n/g' < unixfile.txt > dosfile.txt perl -p -e 's/\n/\r/g' < unixfile.txt > macfile.txt
 * 1) Convert UNIX newlines to DOS/Windows newlines
 * 1) Convert UNIX newlines to Mac newlines

Substitute (find and replace)
perl -pe '/baz/ && s/foo/bar/' sed 's/to_be_replaced/replaced/g' in.txt > out.txt
 * 1) Substitute (find and replace) "foo" with "bar" on lines that match "baz"
 * 1) Sed can do it too

Print the line before a line that matches a regular expression
perl -ne '/regex/ && $last && print $last; $last = $_'

Print the line after a line that matches a regular expression
perl -ne 'if ($p) { print; $p = 0 } $p++ if /regex/'

Print lines that match regex AAA and regex BBB in any order
perl -ne '/AAA/ && /BBB/ && print'

Print lines that don't match match regexes AAA and BBB
perl -ne '!/AAA/ && !/BBB/ && print'

Print lines that match regex AAA followed by regex BBB followed by CCC
perl -ne '/AAA.*BBB.*CCC/ && print'

Print only line 10
perl -ne '$. == 10 && print && exit'

Print all lines except line 10
perl -ne '$. != 10 && print'

Print only lines 10, 15 and 40
perl -ne 'print if $. == 10 || $. == 15 || $. == 40'

Print all lines between two regexes (including lines that match regex)
perl -ne 'print if /regex1/../regex2/'

Print all lines from line 10 to line 30
perl -ne 'print if $. >= 10 && $. <= 30'

Print every second line
perl -ne 'print if $. % 2'

Print every second line, starting the second line
perl -ne 'print if $. % 2 == 0'

Print all lines that repeat
perl -ne 'print if ++$a{$_} == 2'