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 Exclude everything after a character/string from an output in Linux

This is my file:

abc.test.com
efg.test.com:80/test1/123/xyz
xyz.test.com:443/test1
xab.test.com:80
lmn.test.com/100
com.test.com:10

I am trying to remove all characters after the string ".com", but I want to include ".com" in it.
I tried sed ‘s/.com.*//’, however it seems to exclude ".com" as well:

$ cat test1.txt | grep .com | sed 's/.com.*//'
abc.test
efg.test
xyz.test
xab.test
lmn.test
com.test

Is there a way to remove all characters after a particular string, however the output should still have that string it.

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

>Solution :

A common trick is to re-add the deleted part:

sed 's/\.com.*/.com/`
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