1

Could anybody suggest a reg ex for checking versions What if I like to check any update version in 1.0.0 release may be 1.0.0-1 or 1.0.0-2 or 1.0.0-3 and I just need to check for what update version is it -1 or -2 or -3 1.0.0-1 or 1.0.0-2 or 1.0.0-3 what regex can I use ? tried

sed -ne s/1.0.0\-[0-9]\1/p

Thanks !

3 Answers 3

0

Perhaps something like this?:

$ echo "1.0.0-1" | sed -ne 's/1.0.0\(\-[0-9]\)/\1/p'
-1

The regex to get the update number for any version would be:

$ echo "1.0.0-1" | sed -ne 's/[0-9]\+\.[0-9]\+\.[0-9]\+\(\-[0-9]\)/\1/p'
-1

The trick is to capture the output you want between \( and \) grouping parentheses and the use \1 to output the pattern in the replace part of the command.

0

You don't really need a regular expression here; just split the string on the correct character:

version=1.0.0-2
major=$(echo $version | cut -d. -f1 )
minor=$(echo $version | cut -d. -f1 )
patchlvl=$(echo $version | cut -d. -f1 | cut -d - -f1 )
build=$(echo $version | cut -d - -f2)
0

This will leave only the update version number:

echo "1.0.0-1" | sed -e 's/^[1-9][0-9.]*-//'

And this will leave the leading '-' on it:

echo "1.0.0-1" | sed -e 's/^[1-9][0-9.]*//'

The p command isn't needed in the above so I left it, and the -n option off, but you can restore them if this part of some more elaborate sed script.

Edit: OP apparently just wants to know if the version contains an update. Here are a couple of ways in classic bourne shell:

#!/bin/sh
version='1.0.0-1'
case "$version" in
    *-[1-9]*)  # Not a regex, this is a (file) "pattern"
        # Update triggered code here
        ;;
esac

# Or:
if echo "$version" | grep '-[1-9][0-9]*$' >/dev/null ; then
    # Update triggered code here
fi

There are other ways to do it in other shells/environments of course.

3
  • Thanks ! but I was only looking for checking update in script Commented Mar 11, 2013 at 15:39
  • Thanks ! But I was only looking for regex to check something like this if ("1.0.0-any version") "run this command " else don't run e.g $ver = 1.0.0-2 if [ -z "$(echo $ver | awk '/7.0.0-*/')" ]; then date #<or any command> Commented Mar 11, 2013 at 15:45
  • Sorry, it looked like you were trying to do a substitution (that's what the 's' command in sed does) to isolate the update portion of the version.
    – William
    Commented Mar 11, 2013 at 22:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.