LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Sed - Replace all spaces at beginning of line with the number 1. (https://www.linuxquestions.org/questions/programming-9/sed-replace-all-spaces-at-beginning-of-line-with-the-number-1-a-815415/)

felix001 06-21-2010 07:39 AM

Sed - Replace all spaces at beginning of line with the number 1.
 
Does any one know what syntax i could use to allow me to replace all instances at the beginning of a line with ones.

Before :
Code:

----------------------------------------------------------------------
Logical device information
----------------------------------------------------------------------
Logical device number 0
  Logical device name                      : RAID1Mirror
    RAID level                              : 1
    Status of logical device                : Optimal

After
Code:

----------------------------------------------------------------------
Logical device information
----------------------------------------------------------------------
Logical device number 0
111Logical device name                      : RAID1Mirror
1111RAID level                              : 1
11111Status of logical device                : Optimal

Many Thanks,

colucix 06-21-2010 08:06 AM

Code:

sed ':a; s/^\( *\) \([^ ]\)/\11\2/; t a' file
See http://www.grymoire.com/Unix/Sed.html#uh-59 for some details about the t command. HTH.

syg00 06-21-2010 08:22 AM

Sounds like the sort of thing only a teacher would propose ...

felix001 06-21-2010 08:38 AM

I previously said 1`s as i thought i could just replace this with the HTML i needed.

What i actually require is that I replace each space at the beginning of the line with   .

Many Thanks,

druuna 06-21-2010 08:50 AM

Hi,

sed ':a; s/^\( *\) \([^ ]\)/\1\&nbsp\2/; t a' file

colucix 06-21-2010 10:19 AM

Quote:

Originally Posted by felix001 (Post 4010207)
What i actually require is that I replace each space at the beginning of the line with   .

It's not clear what are your sed skills or knowledge, but if you can interpret the command I suggested above, you might also substitute the spaces with whatever you want (as druuna has shown).

pixellany 06-21-2010 10:56 AM

Has anyone tested this?

With only a quick glance, it seems that it replaces "any number of spaces at the beginning of the line" with <newtext> each time thru the loop.

druuna 06-21-2010 11:04 AM

@pixellany: Yes, I have and it works on my side.

I do believe this is based on greediness and works by taking the longest match, changing the last character with the desired match, then jump to the beginning and do it again (and again....), working your way back to the shortest, and last one.

pixellany 06-21-2010 11:12 AM

clank, grind, screeeech.....

(Trying to get brain to start......;) )

AHA!!!
The logic is "any # of spaces" captured in the backref, and then ANOTHER space which gets replaced. Going thru the loop it keeps picking off one space at a time. BUT--when it's down to only one space, how does THAT one get replaced?

druuna 06-21-2010 11:25 AM

@pixellany: ^\( *\) -> zero (!) or more followed by a space followed by a space at the beginning. Both \1 and \2 can be empty and thus the last space can be substituted as well.

BTW: I don't think the whole \2 part is needed: sed ':a; s/^\( *\) /\1\&nbsp/; t a' file seems to work too.

EDIT
The \2 part is needed in some cases and is the safe way to go, see post #12 by colucix
END EDIT

pixellany 06-21-2010 11:34 AM

Saying it another way, when there is only 1 space, the second part of the regex gets it. clever!!

good news? Brain is started.

bad news? This means I have to do my paying job......;)

colucix 06-21-2010 12:19 PM

That's it: at each passage the loop substitutes one single space followed by a non-space character. The second reference is not actually needed, unless in the input there are lines containing only spaces that we don't want to replace. :jawa:

druuna 06-21-2010 12:22 PM

@colucix:
Quote:

The second reference is not actually needed, unless in the input there are lines containing only spaces that we don't want to replace.
Ah, that explains it. Why didn't I think of that....

Like pixellany already stated: Clever!


All times are GMT -5. The time now is 02:38 AM.