How to print a particular character n number of times in a line??

hi,

Is it possible to print a particular character n number of times in a line?
for example.

i am print the following line using echo command..

echo "files successfully moved"

i want to count the number of characters that are been displayed. i am doin it using

echo "files successfully moved"
no=`echo "files successfully moved" | wc -c`

after this i want to put a "-" below just to underline the text printed. like

files successfully moved
-------------------------

so after counting the number of characters printed, i want to print that many number of "-" below the line. how can i do this? plz help.

i=1
while [ $i -le $no ]
do
  echo -n "-"
  (( i++ ))
done
echo

or

echo "files successfully moved" | sed 's/./-/g'
1 Like

This is way to do it using awk:

awk -v i=10 'BEGIN { OFS="-"; $i="-"; print }'

i=10 specifies the amount of characters, you can change it to i=$no

1 Like

try also:

echo "files successfully moved"| awk '1; {gsub(".","-")}1'
1 Like

Another way using pure Bash magic:

out="files successfully moved"
dash="-"
echo -e "${out}\n${out//?/$dash}"
files successfully moved
-----------------------
1 Like

Longhand in OSX 10.7.5 default bash terminal:-

Last login: Mon Sep 16 15:43:08 on ttys000
AMIGA:barrywalker~> text="Files successfully removed..."
AMIGA:barrywalker~> lentext="${#text}"
AMIGA:barrywalker~> echo $text; printf "%0.s-" $( seq 1 1 $lentext ); echo ""
Files successfully removed...
-----------------------------
AMIGA:barrywalker~> _
1 Like

thanks all of you for the reply.
Out of all the solutions, i will go for the last solution since that code is reducing the extra work of counting the number of characters been printed. i.e.

echo "files successfully moved"| awk '1; {gsub(".","-")}1'

the above code displays the characters printed by echo command as well as puts the "-" character below it.

can u explain me the above command?? specially

awk '1; {gsub(".","-")}1'

Or, to do both the echoing and the underlining with one simple pipeline:

echo files successfully moved | sed 'p; s/./-/g'

Regards,
Alister

1 Like

more or less:

awk '1; {gsub(".","-")}1'

awk 'print line; {substitute all characters with dash} print line'