my situation is this – I have a file:
this line(5)
that line(6)
another line(9)
one more(88)
last line(156)
I need to change the values in brackets from that to:
this line(1)
that line(2)
another line(3)
one more(4)
last line(5)
Basically, make the values ordered starting from 1.
Now, here’s my code:
#!/bin/bash
FILE=$1
COUNT=0
echo "Working with $FILE"
if [ -f $FILE ]; then
while read -r line; do
echo ${line/\([0-9]{1,3}\)/\($COUNT\)};
let COUNT++;
done < $FILE
else
echo "File does not exist!"
fi
Here’s what I get:
this line(5))/(0)}
that line(6))/(1)}
another line(9))/(2)}
one more(88))/(3)}
last line(156))/(4)}
What am I doing wrong? How can I specify in regex a number of artbitrary length?
Much appreciated.
change your echo line to this:
Edit: The pattern replacement versions works as well, assuming there isn’t parentheses with numbers anywhere else in the line – to use the pattern replacement and make sure that it only looks at the end of the line, use a
%char after the first/separator, like so: