How to grep for semver


TL;DR: pipe to grep for semver:

grep -Eo '[0-9]{1,}.[0-9]{1,}.[0-9]{1,}'

This post goes over how to grep for semver (semantic versioning).

Prerequisites

Given package.json:

{
  "version": "1.2.3"
}

Grep

Find the line with semver:

cat package.json | grep '[0-9].[0-9].[0-9]'

Output:

  "version": "1.2.3"

But what if the semver is 10.20.30?

{
  "version": "10.20.30"
}

Then the previous grep command won’t work. You’ll need to extend the pattern to include one or more digits:

cat package.json | grep -E '[0-9]{1,}.[0-9]{1,}.[0-9]{1,}'

-E is --extended-regexp, which interprets the pattern as an extended regular expression (forces grep to behave as egrep).

To print only the matching part of the lines, set -o or --only-matching:

cat package.json | grep -Eo '[0-9]{1,}.[0-9]{1,}.[0-9]{1,}'

Output:

10.20.30


Please support this site and join our Discord!