LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 12-04-2009, 01:38 PM   #1
simplified
Member
 
Registered: May 2007
Location: London, UK
Distribution: Kubuntu 9.04 x64 / Ubuntu Server 9.04 x64
Posts: 50

Rep: Reputation: 15
Smile 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
 
Old 12-04-2009, 01:43 PM   #2
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,

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 druuna; 12-04-2009 at 01:46 PM. Reason: Fixed typo's
 
Old 12-04-2009, 03:06 PM   #3
simplified
Member
 
Registered: May 2007
Location: London, UK
Distribution: Kubuntu 9.04 x64 / Ubuntu Server 9.04 x64
Posts: 50

Original Poster
Rep: Reputation: 15
Druuna - you are the man!! Thanks a million
 
Old 12-04-2009, 04:15 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Consider also:
[[:blank:]]
(removes tabs also)
 
Old 12-04-2009, 04:26 PM   #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,

For clarity:

[[:blank:]] -> spaces and tabs only (!)
[[:space:]] -> all (!) whitespace chars.
 
Old 12-04-2009, 04:38 PM   #6
vonbiber
Member
 
Registered: Apr 2009
Distribution: slackware 14.1 64-bit, slackware 14.2 64-bit, SystemRescueCD
Posts: 533

Rep: Reputation: 129Reputation: 129
Quote:
Originally Posted by simplified View Post
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.
Old 12-04-2009, 04:42 PM   #7
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
@vonbiber: You are forgetting the removal of the leading whitespace..... Thread was already solved as well.
 
Old 12-04-2009, 06:45 PM   #8
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by druuna View Post
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?
 
Old 12-05-2009, 03:38 AM   #9
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: 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
 
  


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 substituting string freeindy Programming 3 12-24-2008 05:05 AM
i am missing new lines when substituting with sed in a bash script FIRATYILDIRIM Programming 7 12-15-2008 03:07 PM
substituting multiline string in sed rohit2983 Programming 3 01-31-2007 09:00 AM
Whitespace parsing sed? carl.waldbieser Programming 1 12-12-2005 04:24 PM
need help substituting with sed parker Programming 2 08-08-2003 10:03 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:37 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
Open Source Consulting | Domain Registration