You're viewing a comment by Eszter and its responses.

Eszter Permalink
August 12, 2010, 09:30

Hi!
Nice intro, thanks a lot!

But I still got a question:
How can I replace every "-"-s at the beginning and the end of a line with the same number of "N"-s?

for example:
in:
>taxon_1
----tgattagcat---
>taxon_2
tgattag---tga-tag-

out:
>taxon_1
NNNNtgattagcatNNN
>taxon_2
tgattag---tga-tagN

Comment Responses

Robby Permalink
September 27, 2010, 23:05

Great article!

@Eszter
Nice challenge. There could be a shorter way to do this, but I used the branch technique with labels described above and came up with:

sed -e :a -e 's/^\(-*\)-/\1N/;ta' -e :b -e 's/-\(-*\)$/N\1/;tb'

The first two expressions set a label then loop substituting the dashes inside out. Then I basically mirrored the process for the backside.

$ cat taxon
----tgattagcat---
tgattag---tga-tag-
---tafasf--fasdf--

$ cat taxon | sed -e :a -e 's/^\(-*\)-/\1N/;ta' -e :b -e 's/-\(-*\)$/N\1/;tb'
NNNNtgattagcatNNN
tgattag---tga-tagN
NNNtafasf--fasdfNN
Anirudh Permalink
January 31, 2016, 13:38

/^-/{
h;s/[^-].*//;y/-/N/;G;s/\n-*//
}
/-$/{
h;s/.*[^-]//;y/-/N/;x;G;s/-*\n//
}

Reply To This Comment

(why do I need your e-mail?)

(Your twitter handle, if you have one.)

Type the word "coding_95": (just to make sure you're a human)

Please preview the comment before submitting to make sure it's OK.