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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hi Experts ! I need to replace m

Status
Not open for further replies.

bharix

Programmer
Aug 6, 2002
23
DE
Hi Experts !

I need to replace multiple consecative occurrences of
a charachter with one such charachter.
Can it be done with a very short code or a one liner may be?

Please use ksh, awk or sed.

For Example -
XXXXXXXXXabcdXXXXlmnopXXXXXXXXcddcXXXXXXXXXXXX1212XXXXX
should be
XabcdXlmnopXcddcX1212X

thanks in advance,
bharix
 
Hi bharix,

assuming you have a file multi with more than one line which looks like the one from your example:

$ sed "s/XX*/X/g" multi

Hope that helps,

mrjazz [pc2]



 
Thanks a bunch mrjazz,
It worked !!
 
Well, sed and awk are powerfull.
But for simple task there is more simple commands like 'cut' and 'tr' that are easier to master, and faster and cheaper to run.

For example:
Code:
tr -s XY xy
which (tr)anslate consecutives 'X' characters to (-s)ingle 'x' character and consecutives 'Y' characters to (-s)ingle 'Y' character.

In your example you could use:
Code:
tr -s X X
 
Hi dchoulette - I agree. I liked your soln better. (I think - when I put the contraint of using
awk, sed or ksh in my first requirement - i was not even thinking of other and even better possibilities such as tr , cut etc.)

thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top