Stupid Perl Tricks

Build up your unix command line foo with these gnarly pipelines, mostly involving Perl:

Perl as a Command Line Tool:

Perl can be used on the command line with the -p and -n switches.

The -p switch is the equivalent of this:


LINE:
  while (<>) {
	...		# your program goes here
  } continue {
	print or die "-p destination: $!\n";
  }

The -n switch is the equivalent of this:


LINE:
  while (<>) {
	...		# your program goes here
  }

The -n and -p switches are frequently used with the -e switch which takes a Perl program as an argument. Also useful is the -i switch for editing a file in-place.


perl -i '.backup' -pe '[perl code here]'

...see Perl docs at: http://perldoc.perl.org/perlrun.html

Substitution


perl -pe 's/today/yesterday/g;'

Filter


perl -ne 'if (m/VNG\d\d\d\dH/) {print}'

or


cat sbeams.noa | perl -ne 'if (m/(VNG\d\d\d\d\w+)/) {print "$1\n"}'

Some AWK, too

find all nodes in a sif network file:


cat network.sif | awk '{print $1 "\n" $3}' | sort | uniq > genesInNetwork

find all unique entries in a tab-delimited file:


cat pFuriosusPrimary.anno | awk '{print $1}' | sort | uniq > all-genes

Difference

find all entries in the file all-genes that are not in genesInNetwork:


diff genesInNetwork all-genes | grep '> ' | perl -pe 's/> //'

find all entries in the file genesInNetwork that are not in all-genes:


diff genesInNetwork all-genes | grep '< ' | perl -pe 's/< //'