Reputation: 91
A file has list of unique "tags" and "values" separated by tab. I want to repeat the tag according to the given value. Example of input file:
tag value
AAAAA 2
BBBBB 1
CCCCC 3
DDDDD 5
Expected output File
AAAAA
AAAAA
BBBBB
CCCCC
CCCCC
CCCCC
DDDDD
DDDDD
DDDDD
DDDDD
DDDDD
Could you please tell me the awk/sed command. Thanks a lot.
Upvotes: 2
Views: 97
Reputation: 58478
This might work for you (GNU sed):
sed -r 's/(\S+)\s+(\S+)/seq \2 | sed c\1/e' file
Split the line into arguments for seq
and sed
commands and evaluate.
Upvotes: 1
Reputation: 47189
Doing arithmetic in sed
is a pain, so I would avoid that. awk
and perl
are good choices, you can also straightforwardly do it with bash
:
while read tag value; do
while ((value--)); do
printf "%s\n" "$tag"
done
done < infile
Or as a one-liner:
while read tag value; do while ((value--)); do printf "%s\n" "$tag"; done; done < infile
Output:
AAAAA
AAAAA
BBBBB
CCCCC
CCCCC
CCCCC
DDDDD
DDDDD
DDDDD
DDDDD
DDDDD
Upvotes: 0
Reputation: 77155
Here is a perl
alternate:
$ perl -ane 'print "$F[0]\n"x$F[1]' file
AAAAA
AAAAA
BBBBB
CCCCC
CCCCC
CCCCC
DDDDD
DDDDD
DDDDD
DDDDD
DDDDD
Upvotes: 2
Reputation: 98088
An alternative version for GNU awk:
awk '{while($2--) print $1}'
This is not a good problem to solve with sed. You need to replace a number n
with n
1
's (for example 3 with 111) and print the word as you consume these 1
's.
Upvotes: 6
Reputation: 41460
This awk
should do:
awk '{for (i=1;i<=$2;i++) print $1}' file
AAAAA
AAAAA
BBBBB
CCCCC
CCCCC
CCCCC
DDDDD
DDDDD
DDDDD
DDDDD
DDDDD
It loops the number of times found in column #2
, then print the word in column #1
Upvotes: 3