LinuxQuestions.org
Visit Jeremy's Blog.
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, 02: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, 02:43 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
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 02:46 PM. Reason: Fixed typo's
 
Old 12-04-2009, 04: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, 05:15 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Consider also:
[[:blank:]]
(removes tabs also)
 
Old 12-04-2009, 05:26 PM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

For clarity:

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

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

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
@vonbiber: You are forgetting the removal of the leading whitespace..... Thread was already solved as well.
 
Old 12-04-2009, 07:45 PM   #8
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

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

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


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


All times are GMT -5. The time now is 03:58 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration