Print String N times the number before it

Hey All,

I want want to print a string N times the number N before it.

Like i have "20 hello".

so i want to print

hello
hello
hello
.
.
.
.
. 

20 times..

Please help me.. I am not able o figure out.. how to do the same?

Is this homework?

lol... Not at all...

I was working on some data to display content..

couldn't figure out the right way...

What have you tried then?

Here is one way:

#!/usr/bin/bash

INPUT="20 hello"
set $INPUT

for ((i=0; i < $1; i++))
do
   printf "%s\n"  $2
done

awk way

echo "20 hello" | awk '{ while(b<$1) {b=b+1; print $2}}'

Hi,
@senhia83 and @fpmurphy: And if string is by example "20 Hello foo bar" ?
A solution in bash:

echo "20 Hello foo bar" | { read A B && while ((A--)) ; do echo $B ; done }

Regads.

Hi.

To avoid explicit coding for loops:

$ yes "a b c" | head -n 20 | column
a b c	a b c	a b c	a b c	a b c	a b c	a b c	a b c	a b c	a b c
a b c	a b c	a b c	a b c	a b c	a b c	a b c	a b c	a b c	a b c

For systems like:

OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian 5.0.8 (lenny, workstation) 
yes (GNU coreutils) 6.10
head (GNU coreutils) 6.10
column - ( /usr/bin/column, 2007-11-20 )

Best wishes ... cheers, drl

2 Likes

For a single word, try :

echo "20 hello" | perl -ane 'print (($F[1]."\n") x $F[0])'