sed add space X times

Pattern:
Mary walks at the park every day with her children

sed 's/$/ /'

will make it add 1 space at the end (trailing)

I want to add X ($VARIABLE) number of spaces (which comes from RANDOM)

i.e.
VARIABLE='14'
then it will do:

sed 's/$/              /'

= 14 spaces added at the end.....

Some suggestions how to do that?

I tried to do it adding unicode xA0 but it didn't work

Thanks!

Does it have to be sed ? Try

printf "%s%*s<\n" "Mary walks at the park every day with her children" 14 " "
Mary walks at the park every day with her children              <

bash printf supports the -v var option to set the variable var in one go.

Hello holyearth,

Following may help you in same.

1st solution: WE can give number of time we need to print spaces with printf .

awk '{print $0};END{printf("%14s\n"," ")}' ORS=" " Input_file

2nd solution: With a variable by which we can add how many number of space we want to add at last of the string.

awk -vVAR="14" '{for(i=NF;i<=NF;i++){while(j++<=VAR){A=A OFS};$i=$i A}}1'  Input_file

Thanks,
R. Singh

If you don't have "printf" or "sed" like my Android phone then longhand:-

#!/bin/bash
# spaces.sh
spaces=""
VARIABLE=$(($[ RANDOM % 10 ]+1))
text="Mary walks with her dog."
#
for n in $( seq 1 1 $VARIABLE ); do spaces=$spaces" "; done
#
# At the end.
echo -n "$text$spaces"
# Just a separater colon for clarity.
echo -n ":"
# At the start.
echo "$spaces$text"
# Anywhere you choose.
echo "${text:0:7}$spaces${text:7}"

Results, OSX 10.7.5, default bash terminal.

Last login: Sat Jan 24 19:18:07 on ttys000
AMIGA:barrywalker~> cd Desktop/Code/Shell
AMIGA:barrywalker~/Desktop/Code/Shell> ./spaces.sh
Mary walks with her dog.   :   Mary walks with her dog.
Mary wa   lks with her dog.
AMIGA:barrywalker~/Desktop/Code/Shell> ./spaces.sh
Mary walks with her dog.         :         Mary walks with her dog.
Mary wa         lks with her dog.
AMIGA:barrywalker~/Desktop/Code/Shell> ./spaces.sh
Mary walks with her dog.      :      Mary walks with her dog.
Mary wa      lks with her dog.
AMIGA:barrywalker~/Desktop/Code/Shell> ./spaces.sh
Mary walks with her dog.   :   Mary walks with her dog.
Mary wa   lks with her dog.
AMIGA:barrywalker~/Desktop/Code/Shell> _

In short: this is not possible. The reason is that regular expressions have certain limitations and this is one of them.

If you are interested in a general and very scientific explanation of why this is so: regular expressions are of the same expressive power as regular languages, which themselves are Type-3 languages in the Chomsky-Sch�tzenberger hierarchy. (Look up "Chomsky hierarchy" and "regular language" in Wikipedia or the net for more detail.) An expression of the form <expression> times variable would need a so-called backreference, which is not possible in a regular language because it would not be context-free any more. See the Pumping Lemma, which can be used to prove this.

Having said this: you already got some workarounds of one or the other form. Just be aware that everything you can achieve is more or less clever workarounds for the mentioned limitation.

I hope this helps.

bakunin

1 Like

Hi,
With sed (I just replace space by point for view):

$ echo $VARIABLE 
14
$ echo $string 
Mary walks at the park every day with her children
$ echo $string | sed -e 'h;z;:bc;s/^\.\{,'$VARIABLE'\}$/&\./;tbc;x;G;s/\n\.//'
Mary walks at the park every day with her children..............

Regards

Try also

NB=14
echo "Mary walks at the park every day with her children" |  sed ":A;s/ / /${NB};t;s/$/ /;tA;"

Unfortunately, it counts the spaces in the string as well... that h;z; thing by disedorgue may help with it...

---------- Post updated at 23:23 ---------- Previous update was at 23:04 ----------

Or, simply

VAR="              "
echo "Mary walks at the park every day with her children" |  sed "s/$/${VAR}/" 

---------- Post updated at 23:24 ---------- Previous update was at 23:23 ----------

... which is stupid. make it

echo "Mary walks at the park every day with her children${VAR}"
1 Like

Your method is simplest than mine. Just replace space by the only character possible ('\n') and at the end, replace all '\n' by space:

echo "Mary walks at the park every day with her children" |  sed ":A;s/\n/\n/${NB};tB;s/$/\n/;tA;:B;s/\n/ /g"

Regards.

1 Like

The whole concept of adding a random number of spaces to the end of a string doesn't make any sense to me.

I can see wanting to pad a field with trailing spaces to create a fields that contains exactly X characters:
printf '%-*.*s\n' $X $X "$field_contents"

I can see wanting to pad a field with leading spaces to create a field that contains exactly X characters:
printf '%*.*s\n' $X $X "$field_contents"

I can see wanting to pad a field with trailing spaces to create a field that is at least X characters (but not truncate the field if it is longer):
printf '%-*s\n' $X "$field_contents"

I can see wanting to pdd a field with leading spaces to create a field that is at least X characters (but not truncate the field if it is longer):
printf '%*s\n' $X "$field_contents"

Why do you want to add a random number of space???

Still there is an academical interest if this can be done with sed.
Apart from the s/x/y/num that must go from left to right (so can hardly be used on x$ ,
there is x\{num\} , a special case of x\{min,max\} .
Modified the previous idea, and for Unix sed replaced some ; by newline:

NB=14
echo "Mary walks at the park every day with her children" | sed '
:A
/ \{'"$NB"'\}$/b
s/$/ /;bA'

NB instead of newlines one can use several '-e line':

echo "Mary walks at the park every day with her children" | sed -e ':A' -e '/ \{'"$NB"'\}$/b' -e 's/$/ /;bA' 
2 Likes

lol...
by moment I really complicate my life :slight_smile: