sed add space X times


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed add space X times
# 8  
Old 01-24-2015
Quote:
Originally Posted by RudiC
Try also
Code:
NB=14
echo "Mary walks at the park every day with her children" |  sed ":A;s/ / /${NB};t;s/$/ /;tA;"

Your method is simplest than mine. Just replace space by the only character possible ('\n') and at the end, replace all '\n' by space:
Code:
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.

Last edited by disedorgue; 01-24-2015 at 08:34 PM..
This User Gave Thanks to disedorgue For This Post:
# 9  
Old 01-25-2015
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???
# 10  
Old 01-26-2015
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:
Code:
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':
Code:
echo "Mary walks at the park every day with her children" | sed -e ':A' -e '/ \{'"$NB"'\}$/b' -e 's/$/ /;bA'


Last edited by MadeInGermany; 01-26-2015 at 09:18 AM.. Reason: a line was too many
These 2 Users Gave Thanks to MadeInGermany For This Post:
# 11  
Old 01-26-2015
lol...
by moment I really complicate my life Smilie
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