Rob Rob - 3 months ago 21
Perl Question

Using tr command in awk to replace forward slash with vertical bar

I have a large file that contains some lines that look something like:

>m160505_c100980652550000001823221307061611/31156/269-572


I need to replace all the forward slashes with vertical bars. I think awk is a great way to do this- it is fast and is simple. However, I am not sure how to incorporate tr and | into the command. So far I have the following, but I need help to correct it.

awk '/^>/{tr{/}{|}; next}{print}' < Old.fasta > New.fasta


I am not opposed to writing a script for this, but I feel like awk might be faster way to do it.
Thanks

Answer

awk doesn't have a tr command/function, nor does it have an easy way to get the same effect. Since you tagged this "perl", I'm assuming you're also open to a perl oneliner:

perl -lpe '/^>/ and tr{/}{|}' < Old.fasta > New.fasta

does the same thing that the awk looks like it's trying to do; replace all / with | on lines beginning with >.

Comments