How to group matched patterns in different files

Hi,

I have a master file that i need to split into multiple files based on matched patterns. sample of my data as follows:-

scaff_1     a       e     123       130         c_scaff_100
 scaff_1     a       e     132       138         c_scaff_101
 scaff_1     a       e     140       150         c_scaff_102
 scaff_2     a       e     151       160         c_scaff_109
 scaff_2     a       e     181       200         c_scaff_110
 scaff_2     a       e     223       230         c_scaff_120
 scaff_3     a       e     323       340         c_scaff_200
 scaff_3     a       e     352       359         c_scaff_201
 scaff_3     a       e     390       430         c_scaff_203

i want the result to be generated in 3 different files (based on my input) and the output should look like this:-

scaffR1

scaff_1     a       e     123       130         c_scaff_100
scaff_1     a       e     132       138         c_scaff_101
scaff_1     a       e     140       150         c_scaff_102

scaffR2

scaff_2     a       e     151       160         c_scaff_109
scaff_2     a       e     181       200         c_scaff_110
scaff_2     a       e     223       230         c_scaff_120

scaffR3

scaff_3     a       e     323       340         c_scaff_200
scaff_3     a       e     352       359         c_scaff_201
scaff_3     a       e     390       430         c_scaff_203

i had tried 2 methods as follows:-

1)

awk '/^scaff_/{close("scaffR"f); f++}{print $0 >"scaffR" f}' data.txt

2)

grep '\bscaff_1\b' data.txt > scaffR1.txt

But both did not really give me the result as i expected. The first one save each line to a new file. the second one group them but requires me to do it manually.

would appreciate kind help on this. Thanks..:frowning:

nawk '{close(f);f=$1;sub("_","R",f);print $0 >> f}' date.txt
1 Like

Hi vgersh99,

It works great!!! Thanks so much for your prompt response dan kind help.... :smiley: