Search code examples
linuxedibigdata

How to created two file from one file, using as criterion the number of column


I need to create two files from one file. The condition is: If a number of column in rows of input is equal to 11, the rows will copy to output1. If a number of column in a rows of input is equal to 10, the rows will copy to output2.

example input with less column:

Id_animal Id_SNP Farm Allele
ID01 rs01 A 1
ID02 rs01 1
ID03 rs01 B 2
ID04 rs01 0

In this case, the row 1 and row 3 will go to output1 and row 2 and 3 will go to output2.

output1

ID01 rs01 A 1
ID03 rs01 B 2

output2

ID02 rs01 1
ID04 rs01 0

But in my case, the number of a row is 45927948 and the number max of a column is 11 and min 10.


Solution

  • Using awk you can do it like this awk -f script.awk input and script.awk is:

    NR == 1 { next }
    NF == 4 { print > "output1" }
    NF == 3 { print > "output2" }
    
    • Modify the 4 and 3 in the condition on NF (the number of fields in the line).
    • The first line skips the header.