0

I have a text file with a column containing text/data followed by an abundance column, e.g.

Car 3

Apple 2

In order to subsample these columns I would like to replicate each text column by the amount of the abundance column, e.g.

Car 1

Car 1

Car 1

Apple 1

Apple 1

Any idea how to accomplish this with for example awk?

1

If i understand the question properly you could do this

awk '{for(i=0;i<$NF;i++)print $1,"1\n"}' file

Found a shorter way

awk '{while($2--)print $1,"1\n"}' file
0

try

 awk '{ for (i=$2 ; i ; i--) printf "%s 1\n",$1 ; } ' < text

where

- `for (i=$2 ; i ; i--)` is a loop that starting from second field value decrease to 1
- `printf "%s 1\n",$1`  print first field with a 1
  • Dont need to redirect using < as awk takes files as args. I do like how you looped though :) – user78605 Oct 17 '14 at 7:58
  • @Jidder absolutly no need, this is juste given as a sample. – Archemar Oct 17 '14 at 8:02

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.