4

A regular file contains data organized in columns, separated by space. Based on the value of the second field, I want to split that initial data file into several other files.

The data file has 54,242 rows and 399 columns. Data structure is so:

SNP Chr Pos ID_01 ID_02 ...
rs01 1 200 AA BB ...
rs02 25 10 BA AA ...
rs03 1 30 AA BB ...
rs04 X 90 BB AA ...

My output should consist of each record (or row) in as many file"${chr}" as there are different $chr values in column 2. So for example:

file1 will contain all the records whose 2nd field is 1:

SNP Chr Pos ID_01 ID_02 ...
rs01 1 200 AA BB ...
rs03 1 30 AA BB ...

file25 will contain all the records whose 2nd field is 25:

SNP Chr Pos ID_01 ID_02 ...
rs02 25 10 BA AA ...

And so on...

marked as duplicate by don_crissti, Gilles text-processing Jul 22 '16 at 23:39

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

5

With awk:

awk 'FNR==1 { hdr = $0; next }                     
    {
        if(!f[$2]) { print hdr >"file" $2; f[$2] = 1 }
        print >>"file" $2
    }' data.txt
  • 2
    @Cbhihe The initial question makes no reference to some records having only a single column. Only your edit claims that, and it seems to be contrary to the OP's intent. – Satō Katsura Jul 19 '16 at 21:38
  • +1: My bad, I must have dreamt it or been contaminated by another thread I was looking at in parallel... I see that OP is being edited to reflect the original request. Thanks. – Cbhihe Jul 20 '16 at 6:25

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