'correct return of sed and renaming

I have several files named with different lenght, but have a pattern i can find to used SED. I want to remove a determined set of characters in the middle of it so i can rename them, but i am failing miserably at some points... the example file is:

[Matheus]_203-34510033_20220217111125(237).wav

the name in the begging changes, as well as the value inside the parentheses, that is incremented on each file and soon will be bigger than 3 digits. I need to remove the 6 digits before the parenthesis, and my regex can find them correctly:

s/.*_203.*_202\d{5}\K.+?(?=\()//

regex exemple

I am using the following script so i can rename the file:

#!/bin/bash
for filename in *.wav; do
  newFilename=$(sed -E 's/.*_203.*_202\d{5}\K.+?(?=\()//' <<< "$filename")
  mv "$filename" "$newFilename"
done

when i run the script, i receive the output:

sed: -e expression #1, character 46: Invalid Preceding regular expression.

What is wrong with my sed?


EDIT:

Solution given by The Fourth Bird worked: newFilename=$(sed -E 's/(_203.*_202[0-9]{5})[0-9]+(()/\1\2/' <<< "$filename")



Solution 1:[1]

If you want to match the pattern between parenthesis, the (?=...) syntax is not supported. Your pattern will probably become

s/.*_203.*_202[0-9]{5}.+?\(([^\)]+)\)/\1/

Solution 2:[2]

If you use the Perl based rename command:

$ rename -n 's/.*_203.*_202\d{5}\K.+?(?=\()//' *.wav
rename([Matheus]_203-34510033_20220217111125(237).wav, [Matheus]_203-34510033_20220217(237).wav)

-n option is for dry run, remove it for actual renaming. Also, you don't need the .* at the start of the regexp.

I need to remove the 6 digits before the parenthesis

rename -n 's/\d{6}(?=\()//' *.wav


If you don't have the command installed, check your distribution's repository or you can install it from metacpan: File::Rename.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 GaƩtan RYCKEBOER
Solution 2 Sundeep