The Georgia
The Georgia

Reputation: 1075

Bash regex string variable match

I have the following script i wrote in perl that works just fine. But i am trying to achieve the same thing using bash.

#!/usr/bin/perl

use 5.010;
use strict;

INIT {
    my $string = 'Seconds_Behind_Master: 1';
    my ($s) = ($string =~ /Seconds_Behind_Master: ([\d]+)/);
    if ($s > 10) {
        print "Too long... ${s}";
    } else {
        print "It's ok";
    }
}

__END__

How can i achieve this using a bash script? Basically, i want to be able to read and match the value at the end of the string "Seconds_Behind_Master: N" where N can be any value.

Upvotes: 12

Views: 46093

Answers (3)

Krzysztof Krasoń
Krzysztof Krasoń

Reputation: 27476

You can use a tool for it e.g. sed if you want to stay with regexps:

#!/bin/sh
string="Seconds_Behind_Master: 1"
s=`echo $string | sed -r 's/Seconds_Behind_Master: ([0-9]+)/\1/g'`
if [ $s -gt 10 ]
then
    echo "Too long... $s"
else
    echo "It's OK"
fi

Upvotes: 12

xxfelixxx
xxfelixxx

Reputation: 6602

You can use regular expression in bash, just like in perl.

#!/bin/bash

STRING="Seconds_Behind_Master: "

REGEX="Seconds_Behind_Master: ([0-9]+)"

RANGE=$( seq 8 12 )

for i in $RANGE; do
    NEW_STRING="${STRING}${i}"
    echo $NEW_STRING;

    [[ $NEW_STRING =~ $REGEX ]]
    SECONDS="${BASH_REMATCH[1]}"
    if [ -n "$SECONDS" ]; then
        if [[ "$SECONDS" -gt 10 ]]; then
            echo "Too Long...$SECONDS"
        else
            echo "OK"
        fi
    else
        echo "ERROR: Failed to match '$NEW_STRING' with REGEX '$REGEX'"
    fi
done

Output

Seconds_Behind_Master: 8
OK
Seconds_Behind_Master: 9
OK
Seconds_Behind_Master: 10
OK
Seconds_Behind_Master: 11
Too Long...11
Seconds_Behind_Master: 12
Too Long...12

man bash #BASH_REMATCH

Upvotes: 16

tripleee
tripleee

Reputation: 189397

The specific case of "more than a single digit" is particularly easy with just a pattern match:

case $string in
   *Seconds_Behind_Master: [1-9][0-9]*) echo Too long;;
   *) echo OK;;
esac

To emulate what your Perl code is doing more closely, you can extract the number with simple string substitutions.

s=${string##*Seconds_Behind_Master: }
s=${s%%[!0-9]*}
[ $s -gt 10 ] && echo "Too long: $s" || echo OK.

These are glob patterns, not regular expressions; * matches any string, [!0-9] matches a single character which is not a digit. All of this is Bourne-compatible, i.e. not strictly Bash only (you can use /bin/sh instead of /bin/bash).

Upvotes: 2

Related Questions