Question: I have some video files that I need to subtract from the digit part of them. For example my filename is `'2x042 - So You Want To Be In Pictures.mkv'` But it should be `'2x002 - So You Want To Be In Pictures.mkv'` I have been playing around with rename and sed and havent been able to figure out how to get it to do this subtraction (-40)

Here is what I tried:

 `rename -n -v 's/(\d+){3}.*(\.mkv)/($1-40).$2/e' *`

But that ended up giving me `rename(2x041 - The Return of Sailor Moon.mkv, 2x-39.mkv)` which isn't right. How can I do this?

Answer: 
With Perl's standalone `rename`:

```
rename -n 's/(\d+) /sprintf("%03d ",$1-40)/e' *.mkv

```

Output:

```

rename(2x042 - So You Want To Be In Pictures.mkv, 2x002 - So You Want To Be In Pictures.mkv)

```

Remove `-n` if output looks okay.
You can use `rename` as @Cyrus answered, or **perl** directly.

Test:

```
# list of file
ls file*.txt -1
file.1.txt
file.2.txt
file.3.txt
file.4.txt
file.5.txt
file.6.txt
file.7.txt
file.8.txt
file.9.txt

# increment
perl -le '($old=$_) && s/(\d)(?=\.txt)/${1}+1/eg && print for <*.txt>'
file.2.txt
file.3.txt
file.4.txt
file.5.txt
file.6.txt
file.7.txt
file.8.txt
file.9.txt
file.10.txt

# decrement
perl -le '($old=$_) && s/(\d)(?=\.txt)/${1}-1/eg && print for <*.txt>'
file.0.txt
file.1.txt
file.2.txt
file.3.txt
file.4.txt
file.5.txt
file.6.txt
file.7.txt
file.8.txt

# rename
perl -le '($old=$_) && s/(\d)(?=\.txt)/${1}-1/eg && rename($old,$_) for <*.txt>'
# done
# check the result
!ls
ls file*.txt -1
file.0.txt
file.1.txt
file.2.txt
file.3.txt
file.4.txt
file.5.txt
file.6.txt
file.7.txt
file.8.txt

```

Instead of `txt` use your intended extension ( e.g. `mkv`) and find the proper pattern that matches the desired number in the filename.