1

I have a file of 800+ rows. Each row has a certain number of individuals we have to measure a trait on (n = 3, 5 or 10). I would like to expand the file so each individual has their own row based on the number we needed to measure (n).
For example:

Row     Who/What        Description     n
8001    Maize line      Control         3

Would become:

Row     Who/What        Description     n
8001    Maize line      Control         3
8001    Maize line      Control         3
8001    Maize line      Control         3

I am not proficient in Unix (still learning!), but I get that I'd need to read in the txt/csv file, then copy based on the n in the fourth column. I would not need to repeat the column headers, that was just for organization. Can someone help me with this?

  • 1
    Is it really a CSV (comma separated) file? if not, what is the actual column delimiter? – steeldriver Jul 26 at 14:01
2

try with awk:

awk 'NR==1{ print; next } { repeat=$NF; while (repeat--) print } ' infile

your given sample file is not a .csv file which supposed to be comma separated; you will need to specify what delimiter is used in your input file with awk's -F option.

awk -F, 'NR==1{ print; next } { repeat=$NF; while (repeat--) print } ' infile

answer to the question asked in the comment:

awk 'NR==1{ print; next } { bakup=$1; repeat=$NF; 
            while (repeat--) { $1=bakup"."++dup; print }; dup=0 }' infile
  • You're right, I was trying to be clear in my example so I pasted the tabulated version. Here is my csv: Row,Who/What,Description,Phenotype n 8001,B73xM21_BC1S4,LANMLR45B010,3 Would it be possible append the the repeat number to the first column? 8001 becomes 8001.1, 8001.2, 8001.3, etc? I really appreciate the 'awk' code! I really need to work on my awk'ing, it's so powerful! – Michael Syring Jul 26 at 14:24
  • 1
    Worth revising the original question to reflect this new request. – steve Jul 26 at 15:51
  • @MichaelSyring just updated in answer – αғsнιη Jul 26 at 17:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.