Ubuntu – how to use sed to replace from last 3 characters from string

command linesed

I have the issue to extract the x amount of characters from string and i have the difficulty enlarging the capability of the expression.

I have tried to use the sed command.

If i have the string example.com.db i need to keep "example.com",for that i use

sed 's/\.db//g'

How to change expression so that i can use these kind of files too example.db.db.com.db.

in this case the end result i need is example.db.db.com

Best Answer

you can use:

's/.db$//'

alternatively you can use:

's/...$//'

to remove the last 3 characters and

's/\.[^\.]*$//'

to remove the everything after the last dot