Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use sed (or other command line) to replace a set of characters with a different set

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?

like image 263
Rich Avatar asked Aug 16 '10 03:08

Rich


People also ask

Which sed command is used for replacement?

Replacing all the occurrence of the pattern in a line : The substitute flag /g (global replacement) specifies the sed command to replace all the occurrences of the string in the line.

How do you replace a variable in a file using sed?

How SED Works. In the syntax, you only need to provide a suitable “new string” name that you want to be placed with the “old string”. Of course, the old string name needs to be entered as well. Then, provide the file name in the place of “file_name” from where the old string will be found and replaced.

How do you use a new line character in sed?

If using a sed script, "i\" immediately followed by a literal newline will work on all versions of sed. Furthermore, the command "s/$/This line is new/" will only work if the hold space is already empty (which it is by default).


2 Answers

this is how you do it with sed

$ echo "test ATCG GATC test" | sed 'y/ATCG/TAGC/'
test TAGC CTAG test
like image 75
ghostdog74 Avatar answered Oct 17 '22 05:10

ghostdog74


use tr.
cat file | tr ATCG TAGC

like image 40
frankc Avatar answered Oct 17 '22 06:10

frankc