Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I apply Unix's / Sed's / Perl's transliterate (tr) to only a specific column?

Tags:

bash

sed

perl

I have program output that looks like this (tab delim):

    $ ./mycode somefile 
    0000000000000000000000000000000000      238671
    0000000000000000000000000000000001      0
    0000000000000000000000000000000002      0
    0000000000000000000000000000000003      0
    0000000000000000000000000000000010      0
    0000000000000000000000000000000011      1548.81
    0000000000000000000000000000000012      0
    0000000000000000000000000000000013      937.306

What I want to do is on FIRST column only: replace 0 with A, 1 with C, 2 with G, and 3 with T. Is there a way I can transliterate that output piped directly from "mycode". Yielding this:

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA        238671
...
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACT        937.306
like image 867
neversaint Avatar asked Oct 08 '09 08:10

neversaint


1 Answers

Using Perl:

C:\> ./mycode file | perl -lpe "($x,$y)=split; $x=~tr/0123/ACGT/; $_=qq{$x\t$y}"
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA      238671
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAC      0
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAG      0
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAT      0
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACA      0
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACC      1548.81
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACG      0
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACT      937.306

You can use single quotes in Bash:

  
$ ./mycode file | perl -lpe '($x,$y)=split; $x=~tr/0123/ACGT/; $_="$x\t$y"' 

As @ysth notes in the comments, perl actually provides the command line options -a and -F:

 -a                autosplit mode with -n or -p (splits $_ into @F)
 ...
 -F/pattern/       split() pattern for -a switch (//'s are optional)

Using those:

perl -lawnF'\t' -e '$,="\t"; $F[0] =~ y/0123/ACGT/; print @F'
like image 61
Sinan Ünür Avatar answered Nov 05 '22 12:11

Sinan Ünür