Operating System - HP-UX
1748163 Members
3689 Online
108758 Solutions
New Discussion юеВ

Re: Duplicating Characters

 
SOLVED
Go to solution
Paul Murray_4
Frequent Advisor

Duplicating Characters

Can anyone tell me if there is an easy way to echo out a given number of duplicate characters to the screen (or even better, into a variable) !

I want to be able to echo a set number of spaces based upon the width of the screen as set in ${LINES}, from within a ksh script.

I've tried a quick counting loop to build the text, but this seems to really slow up my script.

I know there is probably a good way to do this with awk/sed etc, but I just cannot think of the best way to do it !!

Any help will be greatly appreciated (as always !!)

Thanks,
Paul.
Hey, nobody knows EVERYthing !!!
10 REPLIES 10
Mike Stroyan
Honored Contributor
Solution

Re: Duplicating Characters

Spaces are particularly easy because you can use a printf field width.

myvar=$(printf "%${LINES}s" " ")

That uses the printf command with a blank string printed into a field of $LINES characters.
Paul Murray_4
Frequent Advisor

Re: Duplicating Characters

Mike, what can I say .....

YOU ARE A STAR !!!!!

Thanks !
Paul.
Hey, nobody knows EVERYthing !!!
curt larson_1
Honored Contributor

Re: Duplicating Characters

sounds like something that might be done with printf

this will right justify your text

cat file |
while read text
do
printf '%${LINES}s\n' $text
done


#print "x" number of spaces
printf '%${x}s' " "

of course the important information of how many spaces (other then based on LINES) and where the spaces should be, you left out of your question.

maybe if you provided some sample text to illustrate what your trying to accomplish better suggestions could be provided.
Hein van den Heuvel
Honored Contributor

Re: Duplicating Characters


if it is only spaces you are after, then you are lucky, as printf from anywhere (awk, shell) can nicely do a variable length empty field:

# awk 'END{printf ("test%*stest\n",40," ")}' /dev/null

For long series of other characters PERL has a neat 'x' function

#perl -e 'print "*"x10 . "\n"'
**********
# perl -e 'print "*"x20 . "\n"'
********************


fwiw,
Hein.
Paul Murray_4
Frequent Advisor

Re: Duplicating Characters

Thanks Curt. One small question, just to up-spec my own knowledge really .....

If I wanted to print hash marks instead of spaces, what would I need to change ?

Regs,
Paul.
Hey, nobody knows EVERYthing !!!
curt larson_1
Honored Contributor

Re: Duplicating Characters

If I wanted to print hash marks instead of spaces, what would I need to change ?

the easy answer is perl, where you could do:
print "#" x $num;

but to avoiding calling perl for each line of your text, you'd want to do everything within perl or

#make a long variable with hashes
# just pick a number bigger then your going to need
myvar=$(perl -e 'print "#" x 256;')

then use printf within your shell script to truncate the variable to the length you want

printf "%{Len}s" $myvar

that way you'll only call perl once.

of course a simply while loop might be even faster then calling perl

x="#"
y=10
while (( y > 0 ))
do
x="$x$x"
y=$(( $y = 1 ))
done
# x is now a long string of #'s
curt larson_1
Honored Contributor

Re: Duplicating Characters

I hope you caught my mistake where
x="$x$x"
y=$(( $y = 1 ))

should really be

x="$x$x"
y=$(( $y - 1 ))

and if your really doing some serious text formatting, using perl is the way to go. it is a feature rich tool which is so much better then the shell. i.e. shell text formatting has just implemented the printf function in the past few years, where perl was designed for formattng text.
Paul Murray_4
Frequent Advisor

Re: Duplicating Characters

Hi Curt,

Thanks again for the help. The text formatting part of the script is pretty minor, so I ended up cheating rather than using perl.

MYVAR="$(echo "$(printf "%${LINES}s" " ")" | sed 's/ /\#/g')"

Thanks again (to all !) for your help.

Paul.
Hey, nobody knows EVERYthing !!!
Mike Stroyan
Honored Contributor

Re: Duplicating Characters

sed is a rather heavy general-case tool for replacing characters. The tr command specializes in it.

MYVAR="$(printf %${LINES}s ' ' | tr ' ' '#')"