---- Perl One-Liners ----

Modifing input

Most of the following examples rely on the -p -i -e flags, which modify text in-place. It is advisable to automatically create a backup file first by appending a suffix to the -i flag.
  1. Remove all double quotes from files:
    perl -p -i.bak -e 's/\"//g' input1 input2 input3
  2. Turn all text into lower-case:
    perl -p -i.bak -e '$_ = lc $_' input*
  3. Change Windows line ending to UNIX style:
    perl -p -i.bak -e 's/\cM/\n/g' input*
  4. Add line numbers to all rows containing text:
    perl -p -i.bak -e 's/^/sprintf("%-5s", ++$i)/e if (/\S/)' input*
  5. Trim lines down to the first 42 letters:
    perl -lne 'print(substr $_, 0, 42)' input > output
  6. Reformat the chromosome identifiers in a fasta file, for example when shortening headers such as '>Sc:Oct_2003;chromosome=1' by replacing anything from the greater-than sign to a number with 'chr':
    perl -p -i.bak -e 's/>.+chromosome=(\d+)/>chr$1/' *fsa
  7. Round numbers to 1 digit after the decimal point:
    perl -MRegexp::Common -F"\t" -lane 'foreach (@F) {$_ = sprintf "%.1f", $_ if (/^$RE{num}{real}$/ and /\./)} print (join "\t", @F)' input > output

    try it out with an example input file