Joni
Joni

Reputation: 375

Print three columns with awk, then sets of three columns

I want to create a number of files from a much larger file, dividing by columns. For example, the header of my larger file looks like this:

Name Chr Position SNP1A SNP1B SNP1C SNP2A SNP2B SNP2C SNP3A SNP3B SNP3C

and I want to create these files:

Name Chr Position SNP1A SNP1B SNP1C

Name Chr Position SNP2A SNP2B SNP2C

Name Chr Position SNP3A SNP3B SNP3C

I've been trying to use awk, but I'm a bit of a novice with it, so my command currently reads:

for ((i=1; i<=440;i++)); do awk -f printindivs.awk inputfile done

Where printindivs.awk is: {print $1 $2 $3 $((3*$i)+1) $((3*$i)+2) $((3*$i)+3))}

The output I'm getting suggests that my way of trying to get the sets of three is wrong: how can I do this?

Thanks

Upvotes: 1

Views: 128

Answers (1)

Chris Seymour
Chris Seymour

Reputation: 85775

You can do this easily will just a simple awk script:

$ awk '{for(i=4;i<=NF;i+=3)print $1,$2,$3,$i,$(i+1),$(i+2) > ("out"++j)}' file

The output files will be in out[1..n]:

$ cat out1
Name Chr Position SNP1A SNP1B SNP1C

$ cat out2
Name Chr Position SNP2A SNP2B SNP2C

$ cat out3
Name Chr Position SNP3A SNP3B SNP3C

Upvotes: 2

Related Questions