How to split files according to a field and edit content
<p>I am not sure if I can do this using unix commands or I need a more complicated code, like python.</p>

<p>I have a big input file with 3 columns - id, different sequences (second column) grouped in different groups (3rd column).</p>

<pre><code>Seq1        MVRWNARGQPVKEASQVFVSYIGVINCREVPISMEN       Group1
Seq2        PSLFIAGWLFVSTGLRPNEYFTESRQGIPLITDRFDSLEQLDEFSRSF  Group1
Seq3        HQAPAPAPTVISPPAPPTDTTLNLNGAPSNHLQGGNIWTTIGFAITVFLAVTGYSF  Group20
</code></pre>

<p>I would like:
split this file according the group id, and create separate files for each group; edit the info in each file, adding a ">" sign in the beginning of the id;  and then create a new row for the sequence</p>

<pre><code>Group1.txt file
&gt;Seq1
MVRWNARGQPVKEASQVFVSYIGVINCREVPISMEN       
&gt;Seq2
PSLFIAGWLFVSTGLRPNEYFTESRQGIPLITDRFDSLEQLDEFSRSF

Group20.txt file
&gt;Seq3
HQAPAPAPTVISPPAPPTDTTLNLNGAPSNHLQGGNIWTTIGFAITVFLAVTGYSF
</code></pre>

<p>How can I do that?  </p>

<p>This shell script should do the trick:</p>

<pre><code>#!/usr/bin/env bash

filename="data.txt"
while read line; do
    id=$(echo "${line}" | awk '{print $1}')
    sequence=$(echo "${line}" | awk '{print $2}')
    group=$(echo "${line}" | awk '{print $3}')
    printf "&gt;${id}\n${sequence}\n" &gt;&gt; "${group}.txt"
done &lt; "${filename}"
</code></pre>

<p>where <code>data.txt</code> is the name of the file containing the original data.</p>

<p>Importantly, the Group-files should not exist prior to running the script.</p>

python unix split