LinuxQuestions.org
Visit Jeremy's Blog.
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, 08: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, 09:06 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
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, 09:22 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 16,130

Rep: Reputation: 2257Reputation: 2257Reputation: 2257Reputation: 2257Reputation: 2257Reputation: 2257Reputation: 2257Reputation: 2257Reputation: 2257Reputation: 2257Reputation: 2257
Sounds like the sort of thing only a teacher would propose ...
 
Old 06-21-2010, 09: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, 09:50 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390
Hi,

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

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
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, 11:56 AM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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, 12:04 PM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390
@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 12:10 PM. Reason: Why it works.
 
Old 06-21-2010, 12:12 PM   #9
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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, 12:25 PM   #10
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390
@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 01:26 PM. Reason: Changes to reflect correct situation.
 
Old 06-21-2010, 12:34 PM   #11
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

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

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
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, 01:22 PM   #13
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 09:21 AM
sed - How do you replace end of line with a space pppaaarrrkkk Programming 7 02-07-2011 12:27 PM
SED - replace line after substing rany Linux - Newbie 2 07-02-2009 02:13 AM
sed: replace same number of characters between tags unihiekka Linux - Newbie 6 12-30-2008 04:51 AM
Shell Script that sorts data with number beginning on each line. sunksullen Programming 12 05-09-2007 04:35 PM

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

All times are GMT -5. The time now is 03:00 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration