128

I want to parse a variable (in my case it's development kit version) to make it dot(.) free. If version='2.3.3', desired output is 233.

I tried as below, but it requires . to be replaced with another character giving me 2_3_3. It would have been fine if tr . '' would have worked.

  1 VERSION='2.3.3' 
  2 echo "2.3.3" | tr . _
  • 11
    No, it doesn't require: echo "2.3.3" | tr -d .. – manatwork Dec 12 '13 at 14:59
  • 1
    @manatwork Great, that works. You can post it as answer. Thanks – prayagupd Dec 12 '13 at 15:04
  • 1
    Lot of good answers. But if I can second-guess the objective, be warned about 2.11.3 and 2.1.13... ;-) Consider adding padding zeroes to numbers. – Rmano Dec 12 '13 at 16:06
  • 1
    @Rmano You mean something like VERSION='2.30.3100'? No matter what just .'s are removed with all of the answers here. – prayagupd Dec 12 '13 at 16:13
  • 2
    @PrayagUpd --- I simply meant that if you will use the number after the conversion for comparisons (as to say if "is this version newer or the same") you should take care of cases like 2.11.3 and 2.1.13 --- they seems the same after dot removal, but clearly 2.11.3 is newer. Also, 2.11.3 is newer than 2.1.14, but comparing 2113 and 2114 will lead to the wrong answer. I remember a bug somewhere for this... – Rmano Dec 12 '13 at 16:19
142

There is no need to execute an external program. bash's string manipulation can handle it (also available in ksh (where it comes from) and zsh):

$ VERSION='2.3.3'
$ echo "${VERSION//.}"
233

(In those shells' manuals you can find this in the parameter expansion section.)

  • 20
    For further detail, the syntax of the above answer is ${string//substring/replacement}, where apparently the lack of the final forward slash and replacement string are interpreted as delete. See here. – sherrellbc Jan 6 '16 at 18:19
  • 5
    Right, man bash says it clearly in the Shell Parameter Expansion section: “${parameter/pattern/string} (…) If string is null, matches of pattern are deleted and the / following pattern may be omitted.” – manatwork Jan 6 '16 at 18:49
  • 1
    My issue was that the version number came like this "1.0.0" and I wanted just the number so follow what @manatwork suggested I changed in: "${VERSIONNUM//'"'}" however I insert even ' ' because otherwise it wouldn't recognise the "" like string to take off. – Alexiscanny Sep 14 '16 at 12:53
  • 1
    @Alexiscanny, you mean literal " are present in the value? I'm afraid this counts as new question, but try to just escape the doublequote: "${VERSIONNUM//\"}" pastebin.com/3ECDtkwH – manatwork Sep 14 '16 at 13:03
  • 1
    Superb!! works on -ash too! – Fr0zenFyr Jan 4 '17 at 11:33
92

By chronological order:

tr/sed

echo "$VERSION" | tr -d .
echo "$VERSION" | sed 's/\.//g'

csh/tcsh

echo $VERSION:as/.//

POSIX shells:

set -f
IFS=.
set -- $VERSION
IFS=
echo "$*"

ksh/zsh/bash/yash (and busybox ash when built with ASH_BASH_COMPAT)

echo "${VERSION//.}"

zsh

echo $VERSION:gs/./
  • 1
    tr -d deleted even single char from the set. For Ex i want to delete DCC_ from "DCC_VersionD", It deletes DCC and D. Expected output : VersionD. Actual Output : Version. sed worked like a charm. :) thanks for the post. – kayle May 21 '18 at 11:30
21

In addition to the successful answers already exists. Same thing can be achieved with tr, with the --delete option.

echo "2.3.3" | tr --delete .

Which will output: 233

  • 6
    on macos the --delete flag is not recognised, but you can use -d instead – Anentropic Apr 5 '18 at 17:40
6

you should try with sed instead

sed 's/\.//g'

4
echo "$VERSION" | tr -cd [:digit:]

That would return only digits, no matter what other characters are present

4

Perl

$ VERSION='2.3.3'                                     
$ perl -pe 's/\.//g' <<< "$VERSION"           
233

Python

$ VERSION='2.3.3'                                     
$ python -c 'import sys;print sys.argv[1].replace(".","")' "$VERSION"
233

If $VERSION only contains digits and dots, we can do something even shorter:

$ python -c 'print "'$VERSION'".replace(".","")'
233

(beware it's a code injection vulnerability though if $VERSION may contain any character).

AWK

$ VERSION='2.3.3'
$ awk 'BEGIN{gsub(/\./,"",ARGV[1]);print ARGV[1]}' "$VERSION"
233

Or this:

$ awk '{gsub(/\./,"")}1' <<< "$VERSION"
233

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.