views:

43

answers:

2

I need to invert a DNA text string by changing all the A's to T's, T's to A's, C->G, and G->C.

Can I elegantly handle this in sed (or other command line) without a whole chain of sed global replace commands?

+3  A: 

use tr. It will be far superior to sed for this job.
cat file | tr ATCG TAGC

edit: Just to clarify why sed is bad at this, you will have a tricky time getting it not to reverse the changes you have made on a prior pass, e.g. on one pass you turn A to T but then on a pass where you want to turn T to A, how do you get sed to ignore the Ts that were originally As? It can maybe be done but I can't think of a nice way. tr does this kind of thing naturally, so long as your mapping is a single char to a single char.

frankc
Oh great, this is just what I was looking for. I originally had a solution where I'd change all the A's and G's to some other character (X and Y) then do another pass where I'd change everything back, but that seemed stupid. Thanks for the help.
Rich
far superior ? not really. And UUOC. `tr ATCG TAGC < file`
ghostdog74
+1  A: 

this is how you do it with sed

$ echo "test ATCG GATC test" | sed 'y/ATCG/TAGC/'
test TAGC CTAG test
ghostdog74
interesting, was not aware of the y option to sed
frankc