Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
12-04-2009, 02:38 PM
|
#1
|
Member
Registered: May 2007
Location: London, UK
Distribution: Kubuntu 9.04 x64 / Ubuntu Server 9.04 x64
Posts: 50
Rep:
|
sed - substituting multiple occurrances of whitespace with a ':'
Hi All
I'm having a few problems removing multiple occurrance of whitespace with a ':'.
Here's the string:
Code:
8 contact_name jsmith
As you can see, there's a lot of whitespace. What I'd like to do is remove the whitespace and substitute a ':' in it's place, so it looks like this:
Code:
8:contact_name:cralph
So, in short I'd like to:
- Remove the whitespace at the front
Substitute the remainding whitespace with a ':'
I've tried:
Code:
cat userlist | sed -e 's/[ ]/:/'
...and:
Code:
cat userlist | sed -e 's/[ ]/:/g'
...but can't quite get it right. Any ideas? This is driving me crazy!
TIA, Simplified
|
|
|
12-04-2009, 02:43 PM
|
#2
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
Hi,
Is this what you're looking for:
sed -e 's/[[:space:]][[:space:]]*/:/g' -e 's/^://' infile
Replaces all sorts of whitespaces (and removes the leading one).
Hope this helps.
Last edited by anon237; 12-04-2009 at 02:46 PM.
Reason: Fixed typo's
|
|
|
12-04-2009, 04:06 PM
|
#3
|
Member
Registered: May 2007
Location: London, UK
Distribution: Kubuntu 9.04 x64 / Ubuntu Server 9.04 x64
Posts: 50
Original Poster
Rep:
|
Druuna - you are the man!! Thanks a million 
|
|
|
12-04-2009, 05:15 PM
|
#4
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
Consider also:
[[:blank:]]
(removes tabs also)
|
|
|
12-04-2009, 05:26 PM
|
#5
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
Hi,
For clarity:
[[:blank:]] -> spaces and tabs only (!)
[[:space:]] -> all (!) whitespace chars.
|
|
|
12-04-2009, 05:38 PM
|
#6
|
Member
Registered: Apr 2009
Distribution: slackware64-15.0
Posts: 546
Rep: 
|
Quote:
Originally Posted by simplified
Here's the string:
Code:
8 contact_name jsmith
As you can see, there's a lot of whitespace. What I'd like to do is remove the whitespace and substitute a ':' in it's place, so it looks like this:
Code:
8:contact_name:cralph
|
ok, you want to replace any occurence of one or more space(s) by a ':'
If it's just spaces (no tabs) this should work
Code:
cat userlist | sed 's/ \{1,\}/:/g'
If there are tabs as well as spaces:
Code:
cat userlist | sed 's/[ \t]\{1,\}/:/g'
|
|
0 members found this post helpful.
|
12-04-2009, 05:42 PM
|
#7
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
@vonbiber: You are forgetting the removal of the leading whitespace..... Thread was already solved as well.
|
|
|
12-04-2009, 07:45 PM
|
#8
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
Quote:
Originally Posted by druuna
Hi,
For clarity:
[[:blank:]] -> spaces and tabs only (!)
[[:space:]] -> all (!) whitespace chars.
|
Ooooops!! 3rd mistake this year.....
Why do I think that is counter-intuitive?
Other than a newline, what is whitespace that is not a space or a tab?
|
|
|
12-05-2009, 04:38 AM
|
#9
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
@pixellany: Besides the space and (horizontal) tab it also removes vertical tabs and formfeeds.
[ \t\r\n\v\f] == [[:space::]]
[ \t] == [[::blank::]]
I can understand the confusion 
|
|
|
All times are GMT -5. The time now is 02:59 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|