714
714

Reputation: 13

How to add two columns with a list of different values

I have a file which looks like this:

893     990     1000    1020    1500    1655 
  0/1   1/1     0/1     .       1/0     .
  .     0/1     .       1/1     1/0     .      
  .     .       1/1     0/1     1/0     1/0

How do I add two columns with different values so that the output can look like this:

ID      Population      893     990     1000    1020    1500    1655 
AD0062  pop1      0/1   1/1     0/1     .       1/0     .
AD0063  pop1      .     0/1     .       1/1     1/0     .      
AD0074  pop1      .     .       1/1     0/1     1/0     1/0

Any hints? Thanks.

Upvotes: 0

Views: 144

Answers (2)

Birei
Birei

Reputation: 36272

Based in the previous answer of Karl Nordström and the comment adding the IDs, I hope this awk program can do the job:

Content of infile:

893     990     1000    1020    1500    1655 
  0/1   1/1     0/1     .       1/0     .
  .     0/1     .       1/1     1/0     .      
  .     .       1/1     0/1     1/0     1/0

Content of script.awk:

BEGIN {
    population = "pop1"
    id = "AD0062,AD0063,AD0065,AD0074,AD0075,AD0076,AD0077,AD0078,AD0082,\
          AD0083,AD0087,AD0091,AD0092,AD0098,AD0099,AD0100"
    split( id, id_arr, /,/ )

    OFS = "\t"
}

FNR == 1 { 
    printf "%s\t%s\t%s\n", "ID", "Population", $0
    next
}

FNR > 1 { 
    printf "%s\t%s\t%s\n", id_arr[ FNR - 1 ], population, $0
}

Run it like:

awk -f script.awk infile

With following output:

ID      Population      893     990     1000    1020    1500    1655 
AD0062  pop1      0/1   1/1     0/1     .       1/0     .
AD0063  pop1      .     0/1     .       1/1     1/0     .      
AD0065  pop1      .     .       1/1     0/1     1/0     1/0

Upvotes: 1

Karl Nordström
Karl Nordström

Reputation: 327

awk '{print "ID\tPopulation\t"$0}' input_file

This is given that ID and Population are fixed.

Upvotes: 1

Related Questions