How to stretch n times the content of a column, according to another column's length (using awk)?

Viewed 43

for parsing purpose I need to stretch the content of the first column n times according to the number of characters of another column, within command line.

Note: Col2 contains mixed types: <>,.[A-Za-z]

I should turn something like this:

cat myFile.tsv 

Col1    Col2
Text1    XXXX
Text2    XX
Text3    XXX

Into this:

cat myNewFile.tsv 

Col1
Text1    
Text1    
Text1    
Text1    
Text2    
Text2 
Text3    
Text3    
Text3   

Do you have any efficient suggestions? Thank you :D !!

1 Answers

With awk:

awk 'NR==1{print $1; next} {for(i=1; i<=length($2); i++){print $1}}' file

NR is current row number.

Output:

Col1
Text1
Text1
Text1
Text1
Text2
Text2
Text3
Text3
Text3
Related