I have a bash script to substitute number values inside a text pattern. Suppose I have a file like this:
word5word
word55word
And I want it to look like this:
word125word
word125word
I have this script to do that:
#!/bin/bash
re='([0-9]|[0-9][0-9])'
while read line
do
new_line=${line/"word"$re"word"/"word"125"word"}
echo "$new_line"
done < /home/tomak/test.txt
For some reason, the 're' expression is not interpretted correctly and it prints the original lines. I just can't figure out why. It works for a singe digit, i.e. re='[0-9]'
.
To formulate the expression, I used information from the Bash Guide for Beginners which states that:
Two regular expressions may be joined by the infix operator "|"; the
resulting regular expression matches any string matching either
subexpression.
But it just doesn't work for me. What am I missing?
Note that I tried to set shopt -s extglob
and formulate the expression like [0-9][0-9]?
but that didn't work either.
I'm on Ubuntu 14.10 and have the stock bash version 4.3.30. I run the script with bash foo.sh
.
Best Answer
The parameter expansion's substitution doesn't work with regular expressions.
extglob
can help you here, but it doesn't work with regular expressions, either.The correct syntax for the expression under
extglob
isi.e. a digit one or more times.
Test:
Output: