save the file_name according to the column headers
2
0
Entering edit mode
6.5 years ago
mittu1602 ▴ 200

Hello, My main file looks like this:

main_file.txt

SNP_name    808727238578_A  808727238578_B  808727238578_C
10:100179863    GG  GG  GG  -
10:100210988    AA  AA  AA  -
10:103572804    AA  AA  AA  -
10:1081944  CC  CC  CC  -
10:110158432    AA  AA  AA  -
10:114078559    GG  GG  GG  -
10:115250272    GG  GG  GG  -
10:11743758 AA  AA  AA  -
10:118437858    GG  GG  GG  -

I want to save files according to their column headers as shown below:

808727238578_A.txt

SNP_name    808727238578_A
10:100179863    GG
10:100210988    AA
10:103572804    AA
10:1081944  CC
10:110158432    AA
10:114078559    GG
10:115250272    GG
10:11743758 AA
10:118437858    GG

808727238578_B.txt

SNP_name    808727238578_B
10:100179863    GG
10:100210988    AA
10:103572804    AA
10:1081944  CC
10:110158432    AA
10:114078559    GG
10:115250272    GG
10:11743758 AA
10:118437858    GG

THANK YOU.

microarray SNP • 1.2k views
ADD COMMENT
3
Entering edit mode
6.5 years ago

Assuming that OP file is tab separated and csvkit and parallel installed:

$ head -1 test.txt | tr '\t' '\n' |sed 1d | parallel "csvcut -tc 1,{} test.txt | sed 's/,/\t/g' > {}.txt"

output files (test.txt is main_file.txt in OP, tab separated):

$ ls
808727238578_A.txt  808727238578_B.txt  808727238578_C.txt  test.txt

output from 808727238578_A.txt (tab separated):

$ cat 808727238578_A.txt 
SNP_name    808727238578_A
10:100179863    GG
10:100210988    AA
10:103572804    AA
10:1081944  CC
10:110158432    AA
10:114078559    GG
10:115250272    GG
10:11743758 AA
10:118437858    GG
ADD COMMENT
2
Entering edit mode
6.5 years ago
5heikki 11k
for((i=2;i<5;i++)); do awk -v i=$i 'BEGIN{OFS=FS="\t"}NR==1{n=$i}{print $1,$i > n".txt"}' file; done
ADD COMMENT
0
Entering edit mode

Thanks it worked very fine. :)

ADD REPLY
0
Entering edit mode

does it need any alterations while running through shell script? because it shows errors :(

ADD REPLY

Login before adding your answer.

Traffic: 1577 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6