0

I have to split matrix file into several files based on column headers.
Input:

       Sample1  Sample2 Sample3 Sample4 Sample5      
id1     GG      GG      GG      GG      GG         
id2     AA      AA      AA      AA      AA            
id3     AA      AG      AA      AA      AA          
id4     AA      AA      AG      AA      AA

Expected Output: (4 seperate files with column header as filename)

Sample1

id1 GG
id2 AA
id3 AA
id4 AA

Sample2

id1 GG
id2 AA
id3 AG
id4 AA

...

2
awk 'NR == 1 { for (i = 1; i <= NF; ++i) h[i] = $i; next }
             { for (i = 2; i <= NF; ++i) print $1, $i >h[i-1] }' file

This awk script first saves the headers into the array h so that h[1] will be the first header etc.

For each of the other lines in the input data, it will then print the first column along with the appropriate column to the correctly named file (the filename for column i in the data will be h[i-1]).

The awk program assumes that there will be enough file-descriptors available to keep all output files open at the same time (there may be issues with hundreds or thousands of columns).

If the file uses tabs for delimiters, then use awk -F '\t' ... so that any spaces are preserved properly in the data.

  • its tab delimited file. as suggested I am using awk -F '\t' , but it is throwing following error : awk: cannot open "" for output (No such file or directory). Also files generated does not have header – LiNi Sep 25 '18 at 10:32
  • @LiNi Is the input shown in the question representative of the actual data? How many taps are there before Sample1 on the first line? If there are two, make the first loop run from i = 2 instead of i = 1. The output in the question does not show any headers on the data. – Kusalananda Sep 25 '18 at 10:48

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.