Alternative content
With the emergence of NGS technologies, and sequencing data most of the bioinformaticians mung and wrangle around massive amounts of genomics text. There are several "standardized" file formats (FASTQ, SAM, VCF, etc.) and some tools for manipulating them (fastx toolkit, samtools, vcftools, etc.), there are still times where knowing a little bit of Perl onliner is extremely helpful.
Perl one-liners are small and awesome Perl programs that fit in a single line of code and they do one thing really well. These things include changing line spacing, numbering lines, doing calculations, converting and substituting text, deleting and printing certain lines, parsing logs, editing files in-place, doing statistics, carrying out system administration tasks, updating a bunch of files at once, and many more. Perl one-liners will make you the shell warrior. Anything that took you minutes to solve, will now take you seconds!
perl -pe '$\="\n"'
#double space a file
perl -pe '$_ .= "\n" unless /^$/'
#double space a file except blank lines
perl -pe '$_.="\n"x7'
#7 space in a line.
perl -ne 'print unless /^$/'
#remove all blank lines
perl -lne 'print if length($_) < 20'
#print all lines with length less than 20.
perl -00 -pe ''
#If there are multiple spaces, delete all leaving one(make the file a single spaced file).
perl -00 -pe '$_.="\n"x4'
#Expand single blank lines into 4 consecutive blank lines
perl -pe '$_ = "$. $_"'
#Number all lines in a file
perl -pe '$_ = ++$a." $_" if /./'
#Number only non-empty lines in a file
perl -ne 'print ++$a." $_" if /./'
#Number and print only non-empty lines in a file
perl -pe '$_ = ++$a." $_" if /regex/'
#Number only lines that match a pattern
perl -ne 'print ++$a." $_" if /regex/'
#Number and print only lines that match a pattern
perl -ne 'printf "%-5d %s", $., $_ if /regex/'
#Left align lines with 5 white spaces if matches a pattern (perl -ne 'printf "%-5d %s", $., $_' : for all the lines)
perl -le 'print scalar(grep{/./}<>)'
#prints the total number of non-empty lines in a file
perl -lne '$a++ if /regex/; END {print $a+0}'
#print the total number of lines that matches the pattern
perl -alne 'print scalar @F'
#print the total number fields(words) in each line.
perl -alne '$t += @F; END { print $t}'
#Find total number of words in the file
perl -alne 'map { /regex/ && $t++ } @F; END { print $t }'
#find total number of fields that match the pattern
perl -lne '/regex/ && $t++; END { print $t }'
#Find total number of lines that match a pattern
perl -le '$n = 20; $m = 35; ($m,$n) = ($n,$m%$n) while $n; print $m'
#will calculate the GCD of two numbers.
perl -le '$a = $n = 20; $b = $m = 35; ($m,$n) = ($n,$m%$n) while $n; print $a*$b/$m'
#will calculate lcd of 20 and 35.
perl -le '$n=10; $min=5; $max=15; $, = " "; print map { int(rand($max-$min))+$min } 1..$n'
#Generates 10 random numbers between 5 and 15.
perl -le 'print map { ("a".."z",”0”..”9”)[rand 36] } 1..8'
#Generates a 8 character password from a to z and number 0 – 9.
perl -le 'print map { ("a",”t”,”g”,”c”)[rand 4] } 1..20'
#Generates a 20 nucleotide long random residue.
perl -le 'print "a"x50'
#generate a string of ‘x’ 50 character long
perl -le 'print join ", ", map { ord } split //, "hello world"'
#Will print the ascii value of the string hello world.
perl -le '@ascii = (99, 111, 100, 105, 110, 103); print pack("C*", @ascii)'
#converts ascii values into character strings.
perl -le '@odd = grep {$_ % 2 == 1} 1..100; print "@odd"'
#Generates an array of odd numbers.
perl -le '@even = grep {$_ % 2 == 0} 1..100; print "@even"'
#Generate an array of even numbers
perl -lpe 'y/A-Za-z/N-ZA-Mn-za-m/' file
#Convert the entire file into 13 characters offset(ROT13)
perl -nle 'print uc'
#Convert all text to uppercase:
perl -nle 'print lc'
#Convert text to lowercase:
perl -nle 'print ucfirst lc'
#Convert only first letter of first word to uppercas
perl -ple 'y/A-Za-z/a-zA-Z/'
#Convert upper case to lower case and vice versa
perl -ple 's/(\w+)/\u$1/g'
#Camel Casing
perl -pe 's|\n|\r\n|'
#Convert unix new lines into DOS new lines:
perl -pe 's|\r\n|\n|'
#Convert DOS newlines into unix new line
perl -pe 's|\n|\r|'
#Convert unix newlines into MAC newlines:
perl -pe '/regexp/ && s/foo/bar/'
#Substitute a foo with a bar in a line with a regexp.