Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

loop to replace repeating characters

Status
Not open for further replies.

tradntele

Programmer
Nov 7, 2001
14
US
I have a file that has repeating commas and I'd like to replace them with a single comma (effectively, creating a csv file). The challenge is that there can be an infinite number of repeating commas. For example,

field1,field2
field1,,field2
field1,,,,,,,,,field2

So how can I do the substitution... I was thinking of using sed within a while loop, but that doesn't seem to work because of output redirection. For instance,

while grep ',,' datafile
do
sed 's/,,/,/g' datafile > updatedfile
done

If it loops, sed would never actually look at the updatedfile to continue the substitution and it would loop forever.
 
's/[,][,]*/,/g'
vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top