I need to turn these headers:

'>>tr|G1PEZ0|G1PEZ0_MYOLU Uncharacterized protein (Fragment) OS=Myotis lucifugus GN=SNCG PE=4 SV=1

into just this:


Please ignore the apostrophe at the beginning.

  • Well, in this particular case I'd try s/^\(.\).*|\([^ ]*\).*$/\1\2/ but I don't know if that is what you are looking for. – Bananguin Feb 23 '15 at 17:55
  • Does the output contain one >? – heemayl Feb 23 '15 at 17:55
  • 2
    awk -F'[| ]' '{sub(/>tr/,""); print $1$3}' – jasonwryan Feb 23 '15 at 18:00
  • @jasonwryan submit that as an answer! much better than my piped awks (-: – KM. Feb 23 '15 at 18:18
  • 1
    @Bananguin Yes: I can see that; that is why I didn't write up my solution (even though awk is a valid approach) as an answer. Further, for text-processing questions, we tend to tolerate if not encourage multiple approaches... – jasonwryan Feb 23 '15 at 19:46

How about this:

str='>>tr|G1PEZ0|G1PEZ0_MYOLU Uncharacterized protein (Fragment) OS=Myotis lucifugus GN=SNCG PE=4 SV=1'
echo "$str" | sed -e 's/ .*//' -e 's/.*|//' -e 's/^/>/'

That uses 3 separate substitutions - delete everything from the 1st space on the line onwards, delete everything up to the last | on the line, and put a > back as the first character of the line.


This can also be done using cut:

input=">>tr|G1PEZ0|G1PEZ0_MYOLU Uncharacterized protein (Fragment) OS=Myotis lucifugus GN=SNCG PE=4 SV=1"
echo "$input" | cut -d" " -f1 | cut -d"|" -f3 | sed "s/^/>/"

With tr and cut:

tr ' |' '>>' <input |cut -d\> -f1,6

...which transforms all spaces and |pipes into > right-angles, then cuts out from among the resultant > -delimited -fields only the 1st and 6th.

With sed...

sed -n 'y/ /\n/;s/.*|/>/;P' <input

Which also y/// transforms all spaces into \newlines, then s///ubstitutes the left-most and longest string that it can which ends with a |pipe into a single > then Prints only up to the first \newline in pattern space.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.