[SOLVED] Regex: in 's' command second part, variable issue
by dedec0 from LinuxQuestions.org on (#55VPM)
Hello,
for the 'rename' command (made in perl, i forgot where i discovered it, but i have kept it in my mail since, so i will always have it at hands, anywhere i may be; can be obtained with me or below^1 ), i am lost on how to separate the variable in the regex substituting names from a literal symbol that must follow that variable, without anything between them. Some terminal output should help you understand what i need. In the code below, before the rename commands, i am creating a folder in your RAM with some files, to reproduce my environment.
Code:$ mkdir -p /dev/shm/dedec0; cd /dev/shm/dedec0
$ touch cap03.01.py cap03.02.31.py cap03.02.32.py \
cap03.02.33.py cap03.03.py cap03.04.34.py \
cap03.04.35.py cap03.04.36.py cap03.04.37.py \
cap03.05.py cap03.06.py cap03.07.py \
cap03.08.310.py cap03.08.38.py cap03.08.39.py
$ rename -vn 's/^cap03.([0-9][0-9]).([0-9])([0-9])/cap03.$1.$20$3/' cap03.0*
Use of uninitialized value $20 in concatenation (.) or string at (eval 2) line 1.
cap03.02.31.py renamed as
cap03.02.1.py
Use of uninitialized value $20 in concatenation (.) or string at (eval 3) line 1.
cap03.02.32.py renamed as
cap03.02.2.py
Use of uninitialized value $20 in concatenation (.) or string at (eval 4) line 1.
cap03.02.33.py renamed as
cap03.02.3.py
Use of uninitialized value $20 in concatenation (.) or string at (eval 6) line 1.
cap03.04.34.py renamed as
cap03.04.4.py
Use of uninitialized value $20 in concatenation (.) or string at (eval 7) line 1.
cap03.04.35.py renamed as
cap03.04.5.py
Use of uninitialized value $20 in concatenation (.) or string at (eval 8) line 1.
cap03.04.36.py renamed as
cap03.04.6.py
Use of uninitialized value $20 in concatenation (.) or string at (eval 9) line 1.
cap03.04.37.py renamed as
cap03.04.7.py
Use of uninitialized value $20 in concatenation (.) or string at (eval 13) line 1.
cap03.08.310.py renamed as
cap03.08.10.py
Use of uninitialized value $20 in concatenation (.) or string at (eval 14) line 1.
cap03.08.38.py renamed as
cap03.08.8.py
Use of uninitialized value $20 in concatenation (.) or string at (eval 15) line 1.
cap03.08.39.py renamed as
cap03.08.9.py
$ rename -vn 's/^cap03.([0-9][0-9]).([0-9])([0-9]).py/cap03.$1.$2 0$3.py/' cap03.0*
cap03.02.31.py renamed as
cap03.02.3 01.py
cap03.02.32.py renamed as
cap03.02.3 02.py
cap03.02.33.py renamed as
cap03.02.3 03.py
cap03.04.34.py renamed as
cap03.04.3 04.py
cap03.04.35.py renamed as
cap03.04.3 05.py
cap03.04.36.py renamed as
cap03.04.3 06.py
cap03.04.37.py renamed as
cap03.04.3 07.py
cap03.08.38.py renamed as
cap03.08.3 08.py
cap03.08.39.py renamed as
cap03.08.3 09.py
$Notice that both 'rename' commands given here do NOT really rename the files. They just say which operation would be done (argument -n does this). This is much useful because it avoids making disasters when we rename sets of important files. It is one of the main big reasons that i love this command!
^,^
So, the first 'rename' command has regex Code:'s/^cap03.([0-9][0-9]).([0-9])([0-9])/cap03.$1.$20$3/'and it considers the $20 as the regex match to use, and it does not exist.
The second 'rename command has a space after $2 to "fix" the problem with the 0 char after the $2. How do i get rid of this space, without the problem showed?
^1 The command source is https://pastebin.com/ne6HY9Ld , and the manual page for it is https://pastebin.com/1hSdRJ2F . To use the commands i did, copy and save the source as an executable file. The manual should be saved with ".1" extension. To be sure you open this manual, just pass the full or relative path to the file, with "./" if you are in its directory. For example: Code:man ./rename.1 should do it right.


for the 'rename' command (made in perl, i forgot where i discovered it, but i have kept it in my mail since, so i will always have it at hands, anywhere i may be; can be obtained with me or below^1 ), i am lost on how to separate the variable in the regex substituting names from a literal symbol that must follow that variable, without anything between them. Some terminal output should help you understand what i need. In the code below, before the rename commands, i am creating a folder in your RAM with some files, to reproduce my environment.
Code:$ mkdir -p /dev/shm/dedec0; cd /dev/shm/dedec0
$ touch cap03.01.py cap03.02.31.py cap03.02.32.py \
cap03.02.33.py cap03.03.py cap03.04.34.py \
cap03.04.35.py cap03.04.36.py cap03.04.37.py \
cap03.05.py cap03.06.py cap03.07.py \
cap03.08.310.py cap03.08.38.py cap03.08.39.py
$ rename -vn 's/^cap03.([0-9][0-9]).([0-9])([0-9])/cap03.$1.$20$3/' cap03.0*
Use of uninitialized value $20 in concatenation (.) or string at (eval 2) line 1.
cap03.02.31.py renamed as
cap03.02.1.py
Use of uninitialized value $20 in concatenation (.) or string at (eval 3) line 1.
cap03.02.32.py renamed as
cap03.02.2.py
Use of uninitialized value $20 in concatenation (.) or string at (eval 4) line 1.
cap03.02.33.py renamed as
cap03.02.3.py
Use of uninitialized value $20 in concatenation (.) or string at (eval 6) line 1.
cap03.04.34.py renamed as
cap03.04.4.py
Use of uninitialized value $20 in concatenation (.) or string at (eval 7) line 1.
cap03.04.35.py renamed as
cap03.04.5.py
Use of uninitialized value $20 in concatenation (.) or string at (eval 8) line 1.
cap03.04.36.py renamed as
cap03.04.6.py
Use of uninitialized value $20 in concatenation (.) or string at (eval 9) line 1.
cap03.04.37.py renamed as
cap03.04.7.py
Use of uninitialized value $20 in concatenation (.) or string at (eval 13) line 1.
cap03.08.310.py renamed as
cap03.08.10.py
Use of uninitialized value $20 in concatenation (.) or string at (eval 14) line 1.
cap03.08.38.py renamed as
cap03.08.8.py
Use of uninitialized value $20 in concatenation (.) or string at (eval 15) line 1.
cap03.08.39.py renamed as
cap03.08.9.py
$ rename -vn 's/^cap03.([0-9][0-9]).([0-9])([0-9]).py/cap03.$1.$2 0$3.py/' cap03.0*
cap03.02.31.py renamed as
cap03.02.3 01.py
cap03.02.32.py renamed as
cap03.02.3 02.py
cap03.02.33.py renamed as
cap03.02.3 03.py
cap03.04.34.py renamed as
cap03.04.3 04.py
cap03.04.35.py renamed as
cap03.04.3 05.py
cap03.04.36.py renamed as
cap03.04.3 06.py
cap03.04.37.py renamed as
cap03.04.3 07.py
cap03.08.38.py renamed as
cap03.08.3 08.py
cap03.08.39.py renamed as
cap03.08.3 09.py
$Notice that both 'rename' commands given here do NOT really rename the files. They just say which operation would be done (argument -n does this). This is much useful because it avoids making disasters when we rename sets of important files. It is one of the main big reasons that i love this command!
^,^
So, the first 'rename' command has regex Code:'s/^cap03.([0-9][0-9]).([0-9])([0-9])/cap03.$1.$20$3/'and it considers the $20 as the regex match to use, and it does not exist.
The second 'rename command has a space after $2 to "fix" the problem with the 0 char after the $2. How do i get rid of this space, without the problem showed?
^1 The command source is https://pastebin.com/ne6HY9Ld , and the manual page for it is https://pastebin.com/1hSdRJ2F . To use the commands i did, copy and save the source as an executable file. The manual should be saved with ".1" extension. To be sure you open this manual, just pass the full or relative path to the file, with "./" if you are in its directory. For example: Code:man ./rename.1 should do it right.