0

I have N number of sequence in a file like this

>668|CAX10446 conserved hypothetical protein 
MSIRGVGGNGNSRIPSHNGDGSNRRSQNTKNKVEDRVRSLYSSRSNENRESPYAVVDVSSMIESTPTSGE
TTRASRGVFSRFQRGLGRVADKVRRAVQRAWSSVSIRRSSATRATESRSSSRTARGASSGYKEYSPSAAR

>669|CAX10447 conserved protein 
MSIRGVGGNGNSRIPSHNGDGSNRRSQNTKNKVEDRVRSLYSSRSNENRESPYAVVDVSSMIESTPTSGE
TTRASRGVFSRFQRGLGRVADKVRRAVQRAWSSVSIRRSSATRATESRSSSRTARGASSGYKEYSPSAAR

or like this

>gi|76167242|gb|AAX50250.1| hypothetical protein CTA_0001 
MSIRGVGGNGNSRIPSHNGDGSNRRSQNTKNKVEDRVRSLYSSRSNENRESPYAVVDVSSMIESTPTSGE
TTRASRGVFSRFQRGLGRVADKVRRAVQRAWSSVSIRR

Now, I need a grep command to delete a particular word or number in first line.

Output should be:

>CAX10446 conserved hypothetical protein 
MSIRGVGGNGNSRIPSHNGDGSNRRSQNTKNKVEDRVRSLYSSRSNENRESPYAVVDVSSMIESTPTSGE
TTRASRGVFSRFQRGLGRVADKVRRAVQRAWSSVSIRRSSATRATESRSSSRTARGASSGYKEYSPSAAR

or

>AAX50250.1| hypothetical protein CTA_0001 [Chlamydia trachomatis A/HAR-13]
MSIRGVGGNGNSRIPSHNGDGSNRRSQNTKNKVEDRVRSLYSSRSNENRESPYAVVDVSSMIESTPTSGE
TTRASRGVFSRFQRGLGRVADKVRRAVQRAWSSVSIRRSSATRAAESRS
SSRTARGASSGYREYSPSAAR

I need a grep command to do this action, I used sed but it removed the first line, the ID, so it's difficult to identify which sequence is which.

3
  • 6
    No, you don't want grep, it's the wrong tool for the job. grep can't modify your files. Please edit your question and explain what part of the ID line you need removed. Do you want to remove everything before the last |? How can we know what to remove?
    – terdon
    Jun 28, 2016 at 11:13
  • Hi All, Thank you.. I just want to remove the character before "|". I just fount out .. I used "sed 's/^.*|/|/' B_304.txt >n.txt" . It gave me proper out put.. thanks
    – Nitha
    Jun 28, 2016 at 11:32
  • That won't give you the right output for your second example. It will only leave ` hypothetical protein CTA_0001 `.
    – terdon
    Jun 28, 2016 at 11:33

2 Answers 2

2

It looks like you want to remove everything before the last | character. However, your second example has a | after the ID and before the hypothetical protein. Presumably, in that case, you want to remove everything before the last | that is not followed by a space.

Here's one way:

$ sed 's/| / /; s/^[^ ]*|/>/' file.pep 
>CAX10446 conserved hypothetical protein 
MSIRGVGGNGNSRIPSHNGDGSNRRSQNTKNKVEDRVRSLYSSRSNENRESPYAVVDVSSMIESTPTSGE
TTRASRGVFSRFQRGLGRVADKVRRAVQRAWSSVSIRRSSATRATESRSSSRTARGASSGYKEYSPSAAR
>AAX50250.1 hypothetical protein CTA_0001 
MSIRGVGGNGNSRIPSHNGDGSNRRSQNTKNKVEDRVRSLYSSRSNENRESPYAVVDVSSMIESTPTSGE
TTRASRGVFSRFQRGLGRVADKVRRAVQRAWSSVSIRR

The s/| / /; removes any | followed by a space. This will remove the | from AAX50250.1|. The s/^[^ ]*|/>/ looks for a > at the beginning of the line (^>) and removes the longest string of non-space ([^ ]) characters until the last | on the line.

3
  • Thank you so much!!! it was help full. I used sed 's/^.*|AAX/|AAX/' CHT_A.fasta >B.txt
    – Nitha
    Jun 28, 2016 at 11:44
  • @Nitha you're welcome. If this answer solved your issue, please take a moment and accept it by clicking on the check mark to the left. That will mark the question as answered and is the way thanks are expressed on the Stack Exchange sites.
    – terdon
    Jun 28, 2016 at 12:10
  • @Terdon..yes your command worked perfectly! i did
    – Nitha
    Jun 28, 2016 at 12:13
0

If you want to do this with grep. You should read 3 line after each empty line and copy it to variable. You should cut last part of variable.

The parser will do;

First reverse string

Second cut string with | and get first

Third reverse string

echo $yourstring |rev |cut -d'|' -f1|rev

BTW this will not remove, only shows you the part. If you want to save output redirect it to file.

echo $yourstring |rev |cut -d'|' -f1|rev > yournewfile
1
  • Thank you Terdon! your command worked perfectly sed 's/| / /; s/^[^ ]*|/>/'. and Thanks Yavuzs for helping me to use grep, i will try..
    – Nitha
    Jun 28, 2016 at 12:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .