LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   removing more than 1 commas with sed perl or awk (http://www.linuxquestions.org/questions/programming-9/removing-more-than-1-commas-with-sed-perl-or-awk-919734/)

sopier 12-20-2011 06:53 PM

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?

crts 12-20-2011 06:59 PM

Try this instead:

sed -r 's/(, *)+/, /g'

sopier 12-20-2011 07:11 PM

Thank you... it works... can you tell me, when I should use "-r" in sed?

crts 12-20-2011 07:30 PM

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

Telengard 12-20-2011 08:17 PM

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


crts 12-20-2011 09:36 PM

Quote:

Originally Posted by Telengard (Post 4554871)
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.

Telengard 12-20-2011 11:11 PM

Quote:

Originally Posted by crts (Post 4554904)
Code:

sed -r 's/(,[[:blank:]]*)+/, /g'
Now they can also be separated by tabs.

Very well reasoned :)


All times are GMT -5. The time now is 01:37 PM.