I've got a set of four directories
English.lproj
German.lproj
French.lproj
Italian.lprj
each of this contains a serie of XMLs named
2_symbol.xml
4_symbol.xml
5_symbol.xml
... and so on ...
I need to rename all of these files into another numerical pattern,
because the code that determined those numbers have changed.
so the new numerical pattern would be like
1_symbol.xml
5_symnol.xml
3_symbol.xml
... and so on ...
so there's no algorithm applicable to determine this serie, because of this
reason I thought about storing the two numerical series into an array.
I was thinking to a quick way of doing it with a simple bash script.
I think that I'd need an array to store the old numerical pattern and another
array to store the new numerical pattern, so that I can perform a cycle to make
# move n_symbol.xml newdir/newval_symbol.xml
any suggestion?
thx n cheers.
-k-
you don't need bash for this, any POSIX-compatible shell will do.
repls="1:4 2:1 4:12 5:3"
for pair in $repls; do
old=${pair%:*}
new=${pair#*:}
file=${old}_symbol.xml
mv $file $new${file#$old}
done
edit: you need to take care of overwriting files. the snippet above clobbers 4_symbol.xml, for example.
for pair in $repls; do
...
mv $file $new${file#$old}.tmp
done
for f in *.tmp; do
mv $f ${f%.tmp}
done
The following script will randomly shuffle the symbol names of all xml files across 'lproj' directories.
#!/bin/bash
shuffle() { # Taken from http://mywiki.wooledge.org/BashFAQ/026
local i tmp size max rand
size=${#array[*]}
max=$(( 32768 / size * size ))
for ((i=size-1; i>0; i--)); do
while (( (rand=$RANDOM) >= max )); do :; done
rand=$(( rand % (i+1) ))
tmp=${array[i]} array[i]=${array[rand]} array[rand]=$tmp
done
}
for file in *lproj/*.xml; do # get an array of symbol names
tmp=${file##*/}
array[$((i++))]=${tmp%%_*}
done
shuffle # shuffle the symbol name array
i=0
for file in *lproj/*.xml; do # rename the files with random symbols
echo mv "$file" "${file%%/*}/${array[$((i++))]}_${file##*_}"
done
Note: Remove the echo in front of the mv when you are satisfied with the results and re-run the script to make the changes permanent.
Script Output
$ ./randomize.sh
mv 1.lproj/1_symbol.xml 1.lproj/16_symbol.xml
mv 1.lproj/2_symbol.xml 1.lproj/12_symbol.xml
mv 1.lproj/3_symbol.xml 1.lproj/6_symbol.xml
mv 1.lproj/4_symbol.xml 1.lproj/4_symbol.xml
mv 2.lproj/5_symbol.xml 2.lproj/14_symbol.xml
mv 2.lproj/6_symbol.xml 2.lproj/1_symbol.xml
mv 2.lproj/7_symbol.xml 2.lproj/3_symbol.xml
mv 2.lproj/8_symbol.xml 2.lproj/7_symbol.xml
mv 3.lproj/10_symbol.xml 3.lproj/10_symbol.xml
mv 3.lproj/11_symbol.xml 3.lproj/11_symbol.xml
mv 3.lproj/12_symbol.xml 3.lproj/2_symbol.xml
mv 3.lproj/9_symbol.xml 3.lproj/8_symbol.xml
mv 4.lproj/13_symbol.xml 4.lproj/13_symbol.xml
mv 4.lproj/14_symbol.xml 4.lproj/15_symbol.xml
mv 4.lproj/15_symbol.xml 4.lproj/9_symbol.xml
mv 4.lproj/16_symbol.xml 4.lproj/5_symbol.xml
Related
I have many tiff files in a directory and must rename, but there are hundreds of them and so it can be cumbersome. Files look something like this:
basefilename_0002_-0.0.mrc
basefilename_0003_3.0.mrc
basefilename_0004_-3.0.mrc
basefilename_0005_-6.0.mrc
basefilename_0006_6.0.mrc
etc...
All I need to do is change the middle part of the file name so that the first 41 .mrc files will be changes to:
basefilename_0001_-0.0.mrc
basefilename_0001_3.0.mrc
basefilename_0001_-3.0.mrc
basefilename_0001_-6.0.mrc
basefilename_0001_6.0.mrc
etc.
and the second 41 batch of .mrc files:
basefilename_0043_-0.0.mrc
basefilename_0044_3.0.mrc
basefilename_0045_-3.0.mrc
basefilename_0046_-6.0.mrc
basefilename_0047_6.0.mrc
Will be renamed to
basefilename_0002_-0.0.mrc
basefilename_0002_3.0.mrc
basefilename_0002_-3.0.mrc
basefilename_0002_-6.0.mrc
basefilename_0002_6.0.mrc
etc.
So essentially I have to parse after the "basefilename_" and before the next "_" and rename so that the numbers are not ascending but just 0001. But I have hundreds of these and I need to make sure that every 41 mrc files are the same number after the basefile name and before the next description.
You can actually do what you need using the native tools bash itself provides without relying on any separate utilities that would require spawning a separate subshell. Bash provides parameter expansion with substring replacement which you can use to replace the text between _????_ with the new text you want (e.g. 0001, ...).
Bash also provides printf -v var that allows you all the formatting flexibility of man 3 print while letting your save the formatted output in var. So, for example, if I have a value 1 which I want to format as 0001 and store in the variable blkno, it is a simple matter of printf -v "%04d" '1'.
Combining that with a simple counter and then using the bash provided modulo operator, you can do what you need with:
#!/bin/bash
ext=${1:-mrc} ## extension of files to select
declare -i blksz=${2:-41} cnt=0 blk=1 ## files per-block, counters
printf -v blkno "%04d" $blk ## format 1st blk as 0001
for i in *.$ext; do ## loop over each file with extension
## test output showing what would be moved, to new name
printf "mv %-28s %s\n" "$i" "${i/_*_/_${blkno}_}"
## mv "$i" "${i/_*_/_${blkno}_}" ## (uncomment for actual move)
(((cnt+1) % blksz == 0)) && { ## check if blksz output
((blk++)) ## increment blk number
printf -v blkno "%04d" $blk ## format as 4-digit w/leading zeros
}
((cnt++)) ## increment count
done
Notice the script takes as its first argument the extension of the files to loop over (default is .mrc) and the number of files to include in each block 41 by default.
Example Input Files
I didn't have your exact files, so I generated something similar with a loop and touch, e.g.
basefilename_0002_-0.0.mrc
basefilename_0003_3.0.mrc
basefilename_0004_-3.0.mrc
basefilename_0005_6.0.mrc
basefilename_0006_-6.0.mrc
basefilename_0007_9.0.mrc
basefilename_0008_-9.0.mrc
basefilename_0009_12.0.mrc
basefilename_0010_-12.0.mrc
basefilename_0011_15.0.mrc
basefilename_0012_-15.0.mrc
basefilename_0013_18.0.mrc
basefilename_0014_-18.0.mrc
basefilename_0015_21.0.mrc
basefilename_0016_-21.0.mrc
basefilename_0017_24.0.mrc
basefilename_0018_-24.0.mrc
basefilename_0019_27.0.mrc
basefilename_0020_-27.0.mrc
basefilename_0021_30.0.mrc
basefilename_0022_-30.0.mrc
basefilename_0023_33.0.mrc
basefilename_0024_-33.0.mrc
basefilename_0025_36.0.mrc
basefilename_0026_-36.0.mrc
basefilename_0027_39.0.mrc
basefilename_0028_-39.0.mrc
basefilename_0029_42.0.mrc
basefilename_0030_-42.0.mrc
basefilename_0031_45.0.mrc
basefilename_0032_-45.0.mrc
basefilename_0033_48.0.mrc
basefilename_0034_-48.0.mrc
basefilename_0035_51.0.mrc
basefilename_0036_-51.0.mrc
basefilename_0037_54.0.mrc
basefilename_0038_-54.0.mrc
basefilename_0039_57.0.mrc
basefilename_0040_-57.0.mrc
basefilename_0041_60.0.mrc
basefilename_0042_-60.0.mrc
basefilename_0043_0.0.mrc
basefilename_0044_-0.0.mrc
basefilename_0045_3.0.mrc
basefilename_0046_-3.0.mrc
basefilename_0047_6.0.mrc
basefilename_0048_-6.0.mrc
basefilename_0049_9.0.mrc
basefilename_0050_-9.0.mrc
basefilename_0051_12.0.mrc
basefilename_0052_-12.0.mrc
basefilename_0053_15.0.mrc
basefilename_0054_-15.0.mrc
basefilename_0055_18.0.mrc
basefilename_0056_-18.0.mrc
basefilename_0057_21.0.mrc
basefilename_0058_-21.0.mrc
basefilename_0059_24.0.mrc
basefilename_0060_-24.0.mrc
basefilename_0061_27.0.mrc
basefilename_0062_-27.0.mrc
basefilename_0063_30.0.mrc
basefilename_0064_-30.0.mrc
basefilename_0065_33.0.mrc
basefilename_0066_-33.0.mrc
basefilename_0067_36.0.mrc
basefilename_0068_-36.0.mrc
basefilename_0069_39.0.mrc
basefilename_0070_-39.0.mrc
basefilename_0071_42.0.mrc
basefilename_0072_-42.0.mrc
basefilename_0073_45.0.mrc
basefilename_0074_-45.0.mrc
basefilename_0075_48.0.mrc
basefilename_0076_-48.0.mrc
basefilename_0077_51.0.mrc
basefilename_0078_-51.0.mrc
basefilename_0079_54.0.mrc
basefilename_0080_-54.0.mrc
basefilename_0081_57.0.mrc
basefilename_0082_-57.0.mrc
basefilename_0083_60.0.mrc
basefilename_0084_-60.0.mrc
basefilename_0085_0.0.mrc
basefilename_0086_-0.0.mrc
basefilename_0087_3.0.mrc
basefilename_0088_-3.0.mrc
basefilename_0089_6.0.mrc
basefilename_0090_-6.0.mrc
basefilename_0091_9.0.mrc
basefilename_0092_-9.0.mrc
basefilename_0093_12.0.mrc
basefilename_0094_-12.0.mrc
basefilename_0095_15.0.mrc
basefilename_0096_-15.0.mrc
basefilename_0097_18.0.mrc
basefilename_0098_-18.0.mrc
basefilename_0099_21.0.mrc
basefilename_0100_-21.0.mrc
Example Use/Output
note: the actual move mv line is commented out to allow you to test the script and adjust as needed before performing the actual move. Uncomment the line beginning with mv when you are satisfied it performs as needed.
The script outputs file moved, the original and new filenames for the file, e.g.
mv basefilename_0002_-0.0.mrc basefilename_0001_-0.0.mrc
mv basefilename_0003_3.0.mrc basefilename_0001_3.0.mrc
mv basefilename_0004_-3.0.mrc basefilename_0001_-3.0.mrc
mv basefilename_0005_6.0.mrc basefilename_0001_6.0.mrc
mv basefilename_0006_-6.0.mrc basefilename_0001_-6.0.mrc
mv basefilename_0007_9.0.mrc basefilename_0001_9.0.mrc
mv basefilename_0008_-9.0.mrc basefilename_0001_-9.0.mrc
mv basefilename_0009_12.0.mrc basefilename_0001_12.0.mrc
mv basefilename_0010_-12.0.mrc basefilename_0001_-12.0.mrc
mv basefilename_0011_15.0.mrc basefilename_0001_15.0.mrc
mv basefilename_0012_-15.0.mrc basefilename_0001_-15.0.mrc
mv basefilename_0013_18.0.mrc basefilename_0001_18.0.mrc
mv basefilename_0014_-18.0.mrc basefilename_0001_-18.0.mrc
mv basefilename_0015_21.0.mrc basefilename_0001_21.0.mrc
mv basefilename_0016_-21.0.mrc basefilename_0001_-21.0.mrc
mv basefilename_0017_24.0.mrc basefilename_0001_24.0.mrc
mv basefilename_0018_-24.0.mrc basefilename_0001_-24.0.mrc
mv basefilename_0019_27.0.mrc basefilename_0001_27.0.mrc
mv basefilename_0020_-27.0.mrc basefilename_0001_-27.0.mrc
mv basefilename_0021_30.0.mrc basefilename_0001_30.0.mrc
mv basefilename_0022_-30.0.mrc basefilename_0001_-30.0.mrc
mv basefilename_0023_33.0.mrc basefilename_0001_33.0.mrc
mv basefilename_0024_-33.0.mrc basefilename_0001_-33.0.mrc
mv basefilename_0025_36.0.mrc basefilename_0001_36.0.mrc
mv basefilename_0026_-36.0.mrc basefilename_0001_-36.0.mrc
mv basefilename_0027_39.0.mrc basefilename_0001_39.0.mrc
mv basefilename_0028_-39.0.mrc basefilename_0001_-39.0.mrc
mv basefilename_0029_42.0.mrc basefilename_0001_42.0.mrc
mv basefilename_0030_-42.0.mrc basefilename_0001_-42.0.mrc
mv basefilename_0031_45.0.mrc basefilename_0001_45.0.mrc
mv basefilename_0032_-45.0.mrc basefilename_0001_-45.0.mrc
mv basefilename_0033_48.0.mrc basefilename_0001_48.0.mrc
mv basefilename_0034_-48.0.mrc basefilename_0001_-48.0.mrc
mv basefilename_0035_51.0.mrc basefilename_0001_51.0.mrc
mv basefilename_0036_-51.0.mrc basefilename_0001_-51.0.mrc
mv basefilename_0037_54.0.mrc basefilename_0001_54.0.mrc
mv basefilename_0038_-54.0.mrc basefilename_0001_-54.0.mrc
mv basefilename_0039_57.0.mrc basefilename_0001_57.0.mrc
mv basefilename_0040_-57.0.mrc basefilename_0001_-57.0.mrc
mv basefilename_0041_60.0.mrc basefilename_0001_60.0.mrc
mv basefilename_0042_-60.0.mrc basefilename_0001_-60.0.mrc
mv basefilename_0043_0.0.mrc basefilename_0002_0.0.mrc
mv basefilename_0044_-0.0.mrc basefilename_0002_-0.0.mrc
mv basefilename_0045_3.0.mrc basefilename_0002_3.0.mrc
mv basefilename_0046_-3.0.mrc basefilename_0002_-3.0.mrc
mv basefilename_0047_6.0.mrc basefilename_0002_6.0.mrc
mv basefilename_0048_-6.0.mrc basefilename_0002_-6.0.mrc
mv basefilename_0049_9.0.mrc basefilename_0002_9.0.mrc
mv basefilename_0050_-9.0.mrc basefilename_0002_-9.0.mrc
mv basefilename_0051_12.0.mrc basefilename_0002_12.0.mrc
mv basefilename_0052_-12.0.mrc basefilename_0002_-12.0.mrc
mv basefilename_0053_15.0.mrc basefilename_0002_15.0.mrc
mv basefilename_0054_-15.0.mrc basefilename_0002_-15.0.mrc
mv basefilename_0055_18.0.mrc basefilename_0002_18.0.mrc
mv basefilename_0056_-18.0.mrc basefilename_0002_-18.0.mrc
mv basefilename_0057_21.0.mrc basefilename_0002_21.0.mrc
mv basefilename_0058_-21.0.mrc basefilename_0002_-21.0.mrc
mv basefilename_0059_24.0.mrc basefilename_0002_24.0.mrc
mv basefilename_0060_-24.0.mrc basefilename_0002_-24.0.mrc
mv basefilename_0061_27.0.mrc basefilename_0002_27.0.mrc
mv basefilename_0062_-27.0.mrc basefilename_0002_-27.0.mrc
mv basefilename_0063_30.0.mrc basefilename_0002_30.0.mrc
mv basefilename_0064_-30.0.mrc basefilename_0002_-30.0.mrc
mv basefilename_0065_33.0.mrc basefilename_0002_33.0.mrc
mv basefilename_0066_-33.0.mrc basefilename_0002_-33.0.mrc
mv basefilename_0067_36.0.mrc basefilename_0002_36.0.mrc
mv basefilename_0068_-36.0.mrc basefilename_0002_-36.0.mrc
mv basefilename_0069_39.0.mrc basefilename_0002_39.0.mrc
mv basefilename_0070_-39.0.mrc basefilename_0002_-39.0.mrc
mv basefilename_0071_42.0.mrc basefilename_0002_42.0.mrc
mv basefilename_0072_-42.0.mrc basefilename_0002_-42.0.mrc
mv basefilename_0073_45.0.mrc basefilename_0002_45.0.mrc
mv basefilename_0074_-45.0.mrc basefilename_0002_-45.0.mrc
mv basefilename_0075_48.0.mrc basefilename_0002_48.0.mrc
mv basefilename_0076_-48.0.mrc basefilename_0002_-48.0.mrc
mv basefilename_0077_51.0.mrc basefilename_0002_51.0.mrc
mv basefilename_0078_-51.0.mrc basefilename_0002_-51.0.mrc
mv basefilename_0079_54.0.mrc basefilename_0002_54.0.mrc
mv basefilename_0080_-54.0.mrc basefilename_0002_-54.0.mrc
mv basefilename_0081_57.0.mrc basefilename_0002_57.0.mrc
mv basefilename_0082_-57.0.mrc basefilename_0002_-57.0.mrc
mv basefilename_0083_60.0.mrc basefilename_0002_60.0.mrc
mv basefilename_0084_-60.0.mrc basefilename_0003_-60.0.mrc
mv basefilename_0085_0.0.mrc basefilename_0003_0.0.mrc
mv basefilename_0086_-0.0.mrc basefilename_0003_-0.0.mrc
mv basefilename_0087_3.0.mrc basefilename_0003_3.0.mrc
mv basefilename_0088_-3.0.mrc basefilename_0003_-3.0.mrc
mv basefilename_0089_6.0.mrc basefilename_0003_6.0.mrc
mv basefilename_0090_-6.0.mrc basefilename_0003_-6.0.mrc
mv basefilename_0091_9.0.mrc basefilename_0003_9.0.mrc
mv basefilename_0092_-9.0.mrc basefilename_0003_-9.0.mrc
mv basefilename_0093_12.0.mrc basefilename_0003_12.0.mrc
mv basefilename_0094_-12.0.mrc basefilename_0003_-12.0.mrc
mv basefilename_0095_15.0.mrc basefilename_0003_15.0.mrc
mv basefilename_0096_-15.0.mrc basefilename_0003_-15.0.mrc
mv basefilename_0097_18.0.mrc basefilename_0003_18.0.mrc
mv basefilename_0098_-18.0.mrc basefilename_0003_-18.0.mrc
mv basefilename_0099_21.0.mrc basefilename_0003_21.0.mrc
mv basefilename_0100_-21.0.mrc basefilename_0003_-21.0.mrc
Look things over and let me know if you have further questions.
I'm sure there's a better way than the following shell script to get what you want, but something like the following should work, assuming the files are sorted as needed:
#!/bin/bash
set -e
count=1
index=1
for p in *.mrc; do
if expr $count == 42 > /dev/null; then
index=`expr $index + 1`
count=1
else
count=`expr $count + 1`
fi
mv $p `echo $p | sed -e "s/\(.*_\)\([0-9]*\)\(_.*\)/\1000${index}\3/"`
done
The sed command above breaks the filename $p into three parts found between the escaped parentheses pairs, \( ... \):
the base filename (e.g. foo_), retained in \1,
the central digits you're modifying, replaced with 000${index} where ${index} expands to 1 for first set of 41 files, 2 for the second set, etc., and
the suffix (e.g. _3.0.mrc), retained in \3
It's not a very robust implementation since you could end up with central digits like 00023 if $index becomes greater than 9, but I think you get the idea for your own implementation.
Instead of sed you can also use Bash string manipulation builtins. See Section 10.1 Manipulating Strings in the Advanced Bash-Scripting Guide.
I need to create Bash script that generates text files named file001.txt through file050.txt
Of those files, all should have this text inserted "This if file number xxx" (where xxx is the assigned file number), except for file007.txt, which needs to me empty.
This is what I have so far..
#!/bin/bash
touch {001..050}.txt
for f in {001..050}
do
echo This is file number > "$f.txt"
done
Not sure where to go from here. Any help would be very appreciated.
#!/bin/bash
for f in {001..050}
do
if [[ ${f} == "007" ]]
then
# creates empty file
touch "${f}.txt"
else
# creates + inserts text into file
echo "some text/file" > "${f}.txt"
fi
done
The continue statement can be used to skip an iteration of a loop and go on to the next -- though since you actually do want to take an operation on file 7 (creating it), it makes just as much sense to have a conditional:
for (( i=1; i<50; i++ )); do
printf -v filename '%03d.txt' "$i"
if (( i == 7 )); then
# create file if it doesn't exist, truncate if it does
>"$filename"
else
echo "This is file number $i" >"$filename"
fi
done
A few words about the specific implementation decisions here:
Using touch file is much slower than > file (since it starts an external command), and doesn't truncate (so if the file already exists it will retain its contents); your textual description of the problem indicates that you want 007.txt to be empty, making truncation appropriate.
Using a C-style for loop, ie. for ((i=0; i<50; i++)), means you can use a variable for the maximum number; ie. for ((i=0; i<max; i++)). You can't do {001..$max}, by contrast. However, this does need meaning to add zero-padding in a separate step -- hence the printf.
Of course, you can costumize the files' name and the text, the key thing is the ${i}. I tried to be clear, but let us know if you don't understand something.
#!/bin/bash
# Looping through 001 to 050
for i in {001..050}
do
if [ ${i} == 007 ]
then
# Create an empty file if the "i" is 007
echo > "file${i}.txt"
else
# Else create a file ("file012.txt" for example)
# with the text "This is file number 012"
echo "This is file number ${i}" > "file${i}.txt"
fi
done
I have the list of committed files on svn stored in a variable as the following:
REPOS="$1"
TXN="$2"
DETAILED_FILES=`svnlook changed -t $TXN $REPOS`
DETAILED_FILES looks like:
U data0.xml A data1.xml UU all_data.xml
How can I remove all change type prefixes? such as U |data0.xml
Also, is it possible to store these in an array?
And can I get the full path of these files by svnlook?
A more proper way would be:
repos=$1
txn=$2
files=()
while read -r _ f; do
files+=( "$f" )
done < <(svnlook -t "$txn" "$repos")
Mind the quotes! (you used quotes where they are useless—yet harmless—but omitted the mandatory ones!).
Yes, just do:
FILES=( $(echo $DETAILED_FILES | cut -c 3-) )
Now FILES is an array and you can access the array elements by iterating over them:
for i in "${FILES[#]}"; do echo "$i"; done
Explicitly, ${FILES[0]} will get you the first element, ${FILES[1]} second and so on.
I am not familiar with svnlook so can not answer your second question.
I have 3 text files.I want to modify the file name of those files using for loop as below .
Please find the files which I have
1234.xml
333.xml
cccc.xml
Output:
1234_R.xml
333_R.xml
cccc_R.xml
Depending on your distribution, you can use rename:
rename 's/(.*)(\.xml)/$1_R$2/' *.xml
Just basic unix command mv work both on move and rename
mv 1234.xml 1234_R.xml
If you want do it by a large amount, do like this:
[~/bash/rename]$ touch 1234.xml 333.xml cccc.xml
[~/bash/rename]$ ls
1234.xml 333.xml cccc.xml
[~/bash/rename]$ L=`ls *.xml`
[~/bash/rename]$ for x in $L; do mv $x ${x%%.*}_R.xml; done
[~/bash/rename]$ ls
1234_R.xml 333_R.xml cccc_R.xml
[~/bash/rename]$
You can use a for loop to iterate over a list of words (e.g. with the list of file names returned by ls) with the (bash) syntax:
for name [ [ in [ word ... ] ] ; ] do list ; done
Then use mv source dest to rename each file.
One nice trick here, is to use basename, which strips directory and suffix from filenames (e.g. basename 333.xml will just return 333).
If you put all this together, the following should work:
for f in `ls *.xml`; do mv $f `basename $f`_R.xml; done
I have a folder containing images and textfiles.
The image name is in this order: tramnummerx.JPG
The text file is in this order : tramnummerx.txt
to be clear: x is a variable number
Inside the .txt is a 4 digit number, for example 6303.
In this case I would like to rename the .JPG file to : tramnummerx-6303.JPG
In my current script I did this:
x=O
extention='*.txt'
for i in 'ls $extention'
do
x = 'expr $x + 1'
y= ??? the command for extracting the number ???
mv tramnummerx.JPG tramnummerx-$y.JPG
done
I tried some things like awk '{print $(NF-1), $NF;}' filename for "y=" but I just ended up deleting my files this way.
Thanks in advance!
SOLUTION:
for i in 'ls $extention'
do
x='expr $x +1'
read y < tramnummer$x.txt
mv tramnummer$x.JPG tramnummer$x-$y.JPG
done
Use regular expression matching to extract the number from the image file name, and read to extract the new number from the corresponding text file. (Some details may need adjusting if I misunderstood your question, but the general approach will stay the same.)
for img in *.JPG; do
[[ $img =~ tramnummer([[:digit:]]+).JPG ]]
x=${BASH_REMATCH[1]}
read y < tramnummer$x.txt
mv tramnummer$x.JPG tramnummer$x-$y.JPG
done
In your existing code, you could use lines like
x=$(( x + 1 ))
y=$(< tramnummer$x.txt )
but the proposed solution is a little cleaner and more idiomatic.