sed add space X times


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed add space X times
# 1  
Old 01-24-2015
sed add space X times

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


Code:
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:
Code:
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!

Last edited by holyearth; 01-24-2015 at 02:36 PM..
# 2  
Old 01-24-2015
Does it have to be sed? Try
Code:
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.
# 3  
Old 01-24-2015
Hello holyearth,

Following may help you in same.

1st solution: WE can give number of time we need to print spaces with printf.
Code:
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.
Code:
awk -vVAR="14" '{for(i=NF;i<=NF;i++){while(j++<=VAR){A=A OFS};$i=$i A}}1'  Input_file

Thanks,
R. Singh

Last edited by RavinderSingh13; 01-24-2015 at 03:05 PM..
# 4  
Old 01-24-2015
If you don't have "printf" or "sed" like my Android phone then longhand:-
Code:
#!/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.
Code:
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> _

# 5  
Old 01-24-2015
Quote:
Originally Posted by holyearth
I want to add X ($VARIABLE) number of spaces (which comes from RANDOM)

i.e.
VARIABLE='14'
then it will do:
Code:
sed 's/$/              /'

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
This User Gave Thanks to bakunin For This Post:
# 6  
Old 01-24-2015
Hi,
With sed (I just replace space by point for view):
Code:
$ 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
# 7  
Old 01-24-2015
Try also
Code:
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
Code:
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
Code:
echo "Mary walks at the park every day with her children${VAR}"


Last edited by RudiC; 01-24-2015 at 06:13 PM..
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed command to replace one value which occurs multiple times

Hi, My Input File : "MN.1.2.1.2.14.1.1" := "MN_13_TM_4" ( 000000110110100100110001111110110110101110101001100111110100011010110111001 ) "MOS.1.2.1.2.13.6.2" := "MOS_13_TM_4" ( 000000110110100100110001111110110110101110101001100111110100011010110111001 ) Like above template,I have... (4 Replies)
Discussion started by: Preeti Chandra
4 Replies

2. Shell Programming and Scripting

Awk/sed - add space between dot and letter

I need to change . into . so that e.g. A.Jbecomes A. JI have tried sed 's/\./\.\ /g' but that didn't work. (9 Replies)
Discussion started by: locoroco
9 Replies

3. Shell Programming and Scripting

Sed replace using same pattern repeating multiple times in a line

Sed replace using same pattern repeating multiple times in a line I have text like below in a file: I am trying to replace the above line to following How can I acheive this? I am able to do it if the occurrence is for 1 time: But If I try like below I am getting like this: I have to... (4 Replies)
Discussion started by: sol_nov
4 Replies

4. Shell Programming and Scripting

Sed or Awk for lines between two strings multiple times and keep the last one

Hi, I am trying to get lines between the last occurrences of two patterns. I have files that have several occurrences of “Standard” and “Visual”. I will like to get the lines between “Standard” and “Visual” but I only want to retain only the last one e.g. Standard Some words Some words Some... (4 Replies)
Discussion started by: damanidada
4 Replies

5. UNIX for Advanced & Expert Users

Sed - add text to start of line if 1st char anything but space

Problem: I have a lot of files, the files first line should always have 4 spaces before any text. Occasionally some of the files will miss the leading spaces and it's a problem. This is only in the first line. So if there are 4 spaces then text, do nothing. If there are not 4 spaces, add 4... (2 Replies)
Discussion started by: Vryali
2 Replies

6. Shell Programming and Scripting

Add missing times

Not sure about the title if someone has a better name for it please lemme know and I will edit the title. I have several (10+ files) which look something like: File 1: 12/28/2009 04:0 8 12/28/2009 04:4 4 12/28/2009 05:0 4 . . . File 2: 12/28/2009 04:1 7 12/28/2009 04:2 3... (2 Replies)
Discussion started by: jstrangfeld
2 Replies

7. Shell Programming and Scripting

Add white space to the end of a line with sed

Im trying to add 5 blank spaces to the end of each line in a file in a sed script. I can figure out who o put the spaces pretty much anywhere else but at the end. thanks Karl (7 Replies)
Discussion started by: karlanderson
7 Replies

8. Shell Programming and Scripting

How to print first two words two times using SED...

I have string as and I want to print first two words as output. (3 Replies)
Discussion started by: Diggi
3 Replies

9. Shell Programming and Scripting

Awk script to add times.

Hey, Im trying to format the last command to tell me just the user names, logins, and the time that they were logged in. So far I got the users logins using a loop that counts the amount of times a user logged in but im not sure how to start the time array. The time im trying to use is the last... (2 Replies)
Discussion started by: Trilogie
2 Replies

10. Shell Programming and Scripting

sed X amount of times - X is dynamic

I'm trying to make a bash shell script that will allow a user to modify another file based on input they give. Maybe someone can see what I'm doing wrong here. I'm still pretty new at this... Let's say my temp file contains this: 0 1 HELLO 3 4 And here's the code: old=(0 1 3 4) new=(zero... (2 Replies)
Discussion started by: Loriel
2 Replies
Login or Register to Ask a Question