LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 12-20-2011, 06:53 PM   #1
sopier
Member
 
Registered: Dec 2011
Location: Jogja, Indonesia
Distribution: Ubuntu
Posts: 33

Rep: Reputation: Disabled
removing more than 1 commas with sed perl or awk


I have many of this pattern in my text:
Code:
Center, , , , , , , , , VLC
How can I transform it to like this?
Code:
Center, VLC
I already try this command:
Code:
sed "s/,//g"
But it will get rid of the commas...

Any suggestions?
 
Old 12-20-2011, 06:59 PM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Try this instead:

sed -r 's/(, *)+/, /g'
 
1 members found this post helpful.
Old 12-20-2011, 07:11 PM   #3
sopier
Member
 
Registered: Dec 2011
Location: Jogja, Indonesia
Distribution: Ubuntu
Posts: 33

Original Poster
Rep: Reputation: Disabled
Thank you... it works... can you tell me, when I should use "-r" in sed?
 
Old 12-20-2011, 07:30 PM   #4
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
The -r option simplifies the use of extended RegEx. It spares you from having to escape characters like '+' when you want to use it as a quantifier.

Read more about the -r option in this post:
http://www.linuxquestions.org/questi...4/#post4491722
 
Old 12-20-2011, 08:17 PM   #5
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 148Reputation: 148
Hmm ...

Code:
$ echo "Center, , , , , , , , , VLC" | sed 's/, \(, \)*/, /g'
Center, VLC
According to the GNU sed manual, postfixed group expressions are required by POSIX but not portable. Weird. I guess that means the above solution might not work in non-GNU implementations of sed.

Probably best to stick with crts' ERE version. I don't see the need for the repetition operator in (, *) though ???

This should be portable to the best of my knowledge.

Code:
$ echo "Center, , , , , , , , , VLC" | awk '{gsub(/(, )+/, ", "); print}'
Center, VLC
 
Old 12-20-2011, 09:36 PM   #6
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by Telengard View Post
I don't see the need for the repetition operator in (, *) though ???
You are correct, it is just a precaution. I was not sure if the commas would always be separated by exactly one space. Even more precautious:
Code:
sed -r 's/(,[[:blank:]]*)+/, /g'
Now they can also be separated by tabs.
 
Old 12-20-2011, 11:11 PM   #7
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 148Reputation: 148
Quote:
Originally Posted by crts View Post
Code:
sed -r 's/(,[[:blank:]]*)+/, /g'
Now they can also be separated by tabs.
Very well reasoned
 
  


Reply

Tags
awk, sed



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 / Awk / removing chars from field 5 only webs Linux - Newbie 11 10-31-2009 03:46 AM
[SOLVED] move a line with either sed/awk/perl script don_wombat_73 Linux - Newbie 9 07-07-2009 01:06 PM
Modify a text files with awk/sed/perl climber75 Programming 15 08-05-2008 03:35 PM
Text replacement question: sed/awk/perl whatever BigRedBall Programming 6 02-05-2008 11:53 AM
SED, AWK or PERL HELP embsupafly Programming 6 08-20-2005 09:07 PM

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

All times are GMT -5. The time now is 07:45 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