Copying a string from file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RodAG
    New Member
    • Mar 2012
    • 6

    Copying a string from file

    Hi there.
    I'm new to Linux, I'm using the 12.04 LTS distribution and I'm having kind of a hard time here.
    I have a file which contains information in the fasta format (a format for biologists), which looks like this:

    >Id string_of_varia ble_length ~Number~Id2
    [string of thousands of characters]

    >Id string_of_varia ble_length ~Number~Id2
    [string of thousands of characters]

    >Id string_of_varia ble_length ~Number~Id2
    [string of thousands of characters]

    where > is a new entry and contains a header in the first line and a protein/nucleic acid sequence in the lines below.
    Now, everything is unique for each entry except for the tilde, which appears before a number of variable digits and before a second Id for each entry.

    I'm interested in making a new file containing a list of the Id2 field (which is also of variable length) and I thought I could use grep, but I've tried and don't know how to tell it to copy only what appears after the second tilde. Any advice on how to perform that task is greatly appreciated. Even if you could point me in the right direction,
    maybe grep isn't the best way to do it?

    Thanks in advance!
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Use grep and sed (or at least I would) The grep isolates the lines you are interested, you pipe the output into sed which picks out the actual item of interest form the line.

    NOTE OF CAUTION: I'm not sure if ~ needs to be escaped as \~ in regular expressions but I think not.

    Something line this should do it.

    grep -E ".*~.*~" | sed "s/.*~.*~\(.*\)/\1/g"

    Comment

    Working...