In a text file test.txt are many lines of text, of which I want to extract a single line matching:
blabla 28.40.00 blabla
I would like to replace the first digit of the middle number (in this case 4) by three. That is, no matter what the middle number is (40, 41, 52, 63 etc), I would like it to be replaced by a number starting with 3 (40 becomes 30, 41 becomes 31, 52 becomes 32, 63 becomes 33 etc).
The following line matches the middle number and replaces it with the number 3:
cat test.txt |awk '/blabla/'|sed -E s_[[:digit:]][[:digit:]]_3_2
output: blabla 28.3.00 blabla
But when I want to replace only the first digit, sed doesn't work:
cat test.txt |awk '/blabla/'|sed -E s_[[:digit:]]\([[:digit:]]\)1_3\1_2
output: blabla 28.40.00 blabla
What am I doing wrong?
To always replace the third digit in the line:
If the number before the
.can have more than 2 digits, here's one workaround:In your second attempt, you should quote the command and use unescaped parenthesis. You also seem to have an extra
1after the capture group.Also, you don't need
awkin this case. You can add filter insedas well: