what is the best way to match specific version number ( or version number ) from file

I did some options , but I think there are other best approach ( as perl one-liner )

   cat generic_version | grep  "$version"
   cat generic_version | grep -F "$version"
   cat generic_version | awk -v ver=$version '/ver/'

generic_version - is a file with a lot of float versions


cat /tmp/generic_version | ......

expected results:

23.092.12 # version approve 

example of file

23.087.23  # version approve 
23.092.12  # version approve 
23.098.45  # version approve 
23.099.12  # version under testing
  • 3
    As far as I know you cannot call 23.092.12 float number.
    – DevilaN
    Feb 21, 2018 at 9:12
  • ok but I not find other name so we call it version number
    – yael
    Feb 21, 2018 at 9:16
  • As all questions about "best": Please qualify what the sorting criteria is: Shortness of the command, shortest execution time, unreadability of the command, etc.
    – U. Windl
    Feb 7 at 9:19

3 Answers 3


At the very least, you'd want to match on $version provided it's not preceded nor followed by a digit or dot to avoid matching on things like 23.092.123 or (though you may also want to also consider cases like 23.092.12-rc1, 23.092.12b, 23.092.12-2...). May be easier with perl:

<generic_version V=$version perl -ne 'print if m{(?<![\d.])\Q$ENV{V}\E(?![\d.])}'

That matches on:

xxx 23.092.12 yyy

But not on

The (?<!...) and (?!...) are respectively negative look behind and look forward regex operators. We use \Q...\E so that the content of the env var is taken as a fixed string instead of a regexp (so . loses its special meaning of matching any character).

If, as per your edit, your version numbers in the input file are always found at the start of the line and always followed by a spacing character, you can simplify it to:

<generic_version V=$version perl -ne 'print if m{^\Q$ENV{V}\E\s}'

Or use awk to match on lines whose first field is the version:

<generic_version V=$version awk '$1 "" == ENVIRON["V"]'

The concatenation with "" is to force a string comparison. Without it, with a $version like 123.4, it would match on 123.40.

To match on the $version being any space delimited field in the input:

<generic_version V=$version perl -ne 'print if m{(?<!\S)^\Q$ENV{V}\E(?!\S)}'

That is $version provided that it's neither preceded nor followed by a non-spacing character (\S).


Awk solution (according to your input file format):

awk -v ver="$version" '$1 == ver' /tmp/generic_version

The output:

23.092.12  # version approve 

Assuming that the file generic_version contains a version number per line and that $version may match one of these lines exactly:

grep -xF "$version" generic_version

The -F flag causes grep to interpret the pattern as a fixed string rather than as a regular expression, while -x forces the match to span the whole line (as if ^ and $ had been used in a regular expression pattern).

After revealing the format of the file in the question:

awk -v v="$version" '$1 == v' generic_version

This will output every line of the file whose first whitespace separated column is identical to the value of the version variable.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .