LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-21-2010, 07:39 AM   #1
felix001
Member
 
Registered: Jan 2009
Posts: 101

Rep: Reputation: 15
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,
 
Old 06-21-2010, 08:06 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.
 
1 members found this post helpful.
Old 06-21-2010, 08:22 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,120

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Sounds like the sort of thing only a teacher would propose ...
 
Old 06-21-2010, 08:38 AM   #4
felix001
Member
 
Registered: Jan 2009
Posts: 101

Original Poster
Rep: Reputation: 15
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,
 
Old 06-21-2010, 08:50 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

sed ':a; s/^\( *\) \([^ ]\)/\1\&nbsp\2/; t a' file
 
Old 06-21-2010, 10:19 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by felix001 View Post
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).
 
1 members found this post helpful.
Old 06-21-2010, 10:56 AM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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.
 
Old 06-21-2010, 11:04 AM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
@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.

Last edited by druuna; 06-21-2010 at 11:10 AM. Reason: Why it works.
 
Old 06-21-2010, 11:12 AM   #9
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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?
 
Old 06-21-2010, 11:25 AM   #10
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
@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

Last edited by druuna; 06-21-2010 at 12:26 PM. Reason: Changes to reflect correct situation.
 
Old 06-21-2010, 11:34 AM   #11
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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......
 
Old 06-21-2010, 12:19 PM   #12
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.
 
Old 06-21-2010, 12:22 PM   #13
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
@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!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sed insert # at the beginning of a line ilo Linux - Newbie 17 12-19-2012 08:21 AM
sed - How do you replace end of line with a space pppaaarrrkkk Programming 7 02-07-2011 11:27 AM
SED - replace line after substing rany Linux - Newbie 2 07-02-2009 01:13 AM
sed: replace same number of characters between tags unihiekka Linux - Newbie 6 12-30-2008 03:51 AM
Shell Script that sorts data with number beginning on each line. sunksullen Programming 12 05-09-2007 03:35 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration