Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Remove specific character that repeats more than once, and leave only one

I’ve looked at answers of how to remove duplicate words for the last 20 minutes, trying to make them work for my use case, but that didn’t work.

I have a small command that deletes special characters and transforms spaces into dashes,

echo "[[[h[el]lo - w{o}rld%^& -text" | tr -d '?$#@;:/\="<>%{}|^~[]&`' | tr ' ' '-'

and that produces the following output

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

hello---world--text

That runs perfectly, but I also want to add something else to that command, maybe another pipe, that removes the dashes that repeat

ex: I want it to transform, from the produced output, to:

hello-world-text

How can I do this in the most POSIX compliant way possible?

PS: Please tell me if there’s a more efficient way to accomplish what I already made there

>Solution :

You can use the -s flag for that:

echo "[[[h[el]lo - w{o}rld%^& -text" | tr -d '?$#@;:/\="<>%{}|^~[]&`' | tr -s ' ' '-'

-s, --squeeze-repeats
              replace each sequence of a repeated character that is listed in the  last
              specified SET, with a single occurrence of that character
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading