Home > database >  SED Expression To Remove Everything After 3rd Dot
SED Expression To Remove Everything After 3rd Dot

Time:07-13

I have a version number that I need to truncate in bash so that I am left with 2.0.0

Below is the string (version number): 2.0.0.1603182415

I have tried this sed expression, but I don't think it's quite right:

echo '2.0.0.1603182415' | sed 's/\..*//3'

Thanks for your help.

CodePudding user response:

Using sed

$ echo '2.0.0.1603182415' | sed 's/\.[^.]*//3'
2.0.0

CodePudding user response:

sed -E 's/([0-9] \.[0-9] \.[0-9] )[0-9.]*/\1/'

This will also work when there are more than two dots in the version:

echo '2.0.0.1603182415' | sed -E 's/([0-9] \.[0-9] \.[0-9] )[0-9.]*/\1/'
2.0.0
echo '2.0.0.16031.82415' | sed -E 's/([0-9] \.[0-9] \.[0-9] )[0-9.]*/\1/'
2.0.0
echo '2.0.0.16031.824.15' | sed -E 's/([0-9] \.[0-9] \.[0-9] )[0-9.]*/\1/'
2.0.0
  • Related