[SOLVED] Using sed to substitute repetition -> single occurrence
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
There are lots of things sed can do, but I don't know that this problem is best suited for it. Though, some sed guru may come along with a quick one liner.
That said, my immediate thought for this problem would use tr and uniq. If the line of text is in a shell variable for instance, substitute newlines for the spaces, then send the result through uniq, and then substitute spaces for the newlines.
The result should be a line where any repeated block of non-space characters is reduced to a single occurrence.
There may be some input "sanitizing" that may need to be done (i.e. convert multiple spaces to a single space). Also, this approach would not properly handle a repeated sequence that spans multiple lines. Then again, neither would sed without some additional complication.
EDIT:
Oh... yeah, I'm looking at this from the more general perspective that you do not know the exact text that will be repeated beforehand.
EDIT2:
Since my response feels naked without an example:
It's exactly what I wanted (last post, the one before this). Is it possible to make this more generic, so that any repetition, the repetition of any character + space is replaced by the one character?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.