To negate a range in a regular expression, the caret must be inside the square brackets.
sed -i -r 's/$old_string([^0-9])+/$new_string/g' $FILENAME
The parentheses are also unnecessary in this case if you are not using backreferences (and it doesn't look like you are.)
sed -i -r 's/$old_string[^0-9]+/$new_string/g' $FILENAME
One last thing: bash doesn't parse variables inside single-quotes, so $old_string
and $new_string
will not be replaced by any bash variable you may have set in your script but will be used literally in that sed
command. If you use double-quotes, they will be replaced.
sed -i -r "s/$old_string[^0-9]+/$new_string/g" $FILENAME
Update:
Since the file doesn't contain anything after the string you want to replace, the [^0-9]+
part of the regex has nothing to match. It would work if there was a space after the IP address. We can match the end of the line instead with $
.
sed -i -r 's/123.123.123.1$/4.4.4.4/g' $FILENAME
One more update. This one matches any character that isn't a number or no characters at all. For this one we need the parentheses to group the two options.
sed -i -r 's/123.123.123.1([^0-9]+|$)/4.4.4.4/g' $FILENAME
Since we have the parentheses, you can use a backreference to include whatever was matched:
sed -i -r 's/123.123.123.1([^0-9]+|$)/4.4.4.4\1/g' $FILENAME