Rob Rob - 1 year ago 122
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:


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.

Answer Source

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 >.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download