Sed - Replace all spaces at beginning of line with the number 1.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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).
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.
Last edited by druuna; 06-21-2010 at 11:10 AM.
Reason: Why it works.
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?
@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\ /; t a' fileseems 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
Last edited by druuna; 06-21-2010 at 12:26 PM.
Reason: Changes to reflect correct situation.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.