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

How to remove string between two chracters and before the first occurance using sed

I would like to remove the string between ":" and the first "|" using sed.

input:
|abc:1.2.3|def|

output from sed:
|abc|def|

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

I managed to come up with sed 's|\(:\)[^|]*|\1|', but this sed command does not remove the first character (":"). How can I modify this command to also remove the colon?

>Solution :

You don’t need to group : in your pattern and use it in substitution.

You should keep it simple:

s='|abc:1.2.3|def|'
sed 's/:[^|]*//' <<< "$s"

|abc|def|

: matches a colon and [^|]* matches 0 or more non-pipe characters

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