1

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.

1

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 is

re='+([0-9])'

i.e. a digit one or more times.

Test:

re='+([0-9])'
echo $'word5word\nword55word' | while read line ; do
    new_line=${line/"word"$re"word"/"word"125"word"}
    echo "$new_line"
done

Output:

word125word
word125word
0

If you are just substituting in patterns, I suggest sed or perl instead of bash:

sed -e 's/word[0-9]\+word/word125word/g' /home/tomak/test.txt

or

perl -pe 's/word\d+word/word125waord/g' /home/tomak/test.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.