single line perl script
perl -pe '($_)=/([0-9]+([.][0-9]+)+)/' YOURFILE.TXT
or a little longer, but it processes several versions:
perl -nle 'print $v if ($v)=/([0-9]+([.][0-9]+)+)/' YOURFILE.TXT
examples using the first command line
$> V=$(perl -pe '($_)=/([0-9]+([.][0-9]+)+)/'<<<'adcheck (CentrifyDC 4.4.3-421)') $> echo "$V" 4.4.3 $> V=$(perl -pe '($_)=/([0-9]+([.][0-9]+)+)/' <<< 'dos2unix-8.2.3-beta.zip') $> echo "$V" 8.2.3 $> echo 'A2 33. Z-0.1.2.3.4..5' | perl -pe '($_)=/([0-9]+([.][0-9]+)+)/' 0.1.2.3.4 $> gcc --version | perl -pe '($_)=/([0-9]+([.][0-9]+)+)/' 4.7.3 $> bash --version | perl -pe '($_)=/([0-9]+([.][0-9]+)+)/' 4.2.45 $> meld --version | perl -pe '($_)=/([0-9]+([.][0-9]+)+)/' 1.6.1 $> uname -a | perl -pe '($_)=/([0-9]+([.][0-9]+)+)/' 3.8.0 $> perl -pe '($_)=/([0-9]+([.][0-9]+)+)/' /etc/issue 13.04
Note. the first command line retrieves the version without a final carriage return (I mean \n ). If this is a problem, use the second version or complete the first command line in echo :
$> echo $( perl --version | perl -pe '($_)=/([0-9]+([.][0-9]+)+)/' ) 5.14.2
Unfortunately, if there are several lines containing version numbers, these version numbers will be merged:
$> echo $(gwenview --version | perl -pe '($_)=/([0-9]+([.][0-9]+)+)/' )
4.8.44.10.54.10.4
To extract one version for each line, you need to modify a single-line script:
perl -pe 'if(($_)=/([0-9]+([.][0-9]+)+)/){$_.="\n"}'
Example:
$> gwenview --version | perl -pe 'if(($_)=/([0-9]+([.][0-9]+)+)/){$_.="\n"}' 4.8.4 4.10.5 4.10.4
To extract only the first version number and add a carriage return (I mean the new line \n ), I advise:
perl -pe 'if(($v)=/([0-9]+([.][0-9]+)+)/){print"$v\n";exit}$_=""'
example:
$> gwenview --version | perl -pe 'if(($v)=/([0-9]+([.][0-9]+)+)/){print"$v\n";exit}$_=""' 4.8.4
For information, sed is not so good. Who knows how to get the same behavior as my perl scripts, but using sed ?
sed 's/\([0-9][0-9]*[.][0-9][0-9.]*\)/\n\1/'
If you think you can improve these scenarios, feel free to leave comments :)