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> _
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.
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..............
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)
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)
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)
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)
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)
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)
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)