increase/decrease multiple numbers in a filename.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting increase/decrease multiple numbers in a filename.
# 1  
Old 05-17-2012
increase/decrease multiple numbers in a filename.

I got a game that output map tiles of the session with the 0,0 position at the place you login/spawn.
That makes making a map somewhat troublesome since the 0,0 will move.

So I've been looking for a way to change the numbers in the filenames of all files in a folder by a certain value.

The tiles are named "tile_X_Y.png".

I tried to use awk to get the X and Y numbers out, add the values to them and then rename the file, but are not quite sure how to manage that.

I'm not really sure how to use one awk's output as another awk's input

I have edited the code since posted.
Code:
#! /bin/sh  
v1c=5 
echo $v1c 
v2c=-5 
echo $v2c 
cd ./tiles 
for tile in * 
do 
echo "start" 
n_tile=`(awk -F '.' '{print $1}')` //Will remove .png
echo "1" 
v1=`echo $n_tile | awk -F '_' '{print $2}'` //Will take the X
echo "2" 
v2=`echo $n_tile | awk -F '_' '{print $3}'` //Will take the Y
echo "3"  
move="tile_"+$v1+v1c+"_"+v2+v2c+".png" 
echo $move  
mv $tile $move 
done

It feels like there must be a way that's more effective than this script, and I don't really know how to get this script to work. Smilie

Thanks in advance.

Last edited by Ravenholdt; 05-17-2012 at 07:46 PM..
# 2  
Old 05-17-2012
Probably. Reverse-engineering that script -- which I'm not sure even works, and have no way to test without your input and output -- would take a lot more time than it would starting from the input you have and the output you want, however. Could you post it?
# 3  
Old 05-17-2012
Try this (remove echo when your confident it's going to do what you want):

Code:
#!/bin/sh
v1c=5
v2c=-5
cd ./tiles
for tile in *
do
  echo mv $tile `echo $tile | awk -vv1=$v1c -vv2=$v2c -F[_.] '{print $1 "_" $2+v1 "_" $3+v2 ".png" }'`
done

This User Gave Thanks to Chubler_XL For This Post:
# 4  
Old 05-17-2012
I'm afraid I'm not sure what you mean.

But if I have a file called "tile_10_-5.png" I want to be able to add/subtract to the two numbers (10 and -5).

And it should add/subtract that to every filename in the folder.

So if I wanted to add -5 and 3 to the two it should be "tile_5_-2.png" in that example.


If you mean the script, it does not work at the moment, it stops at the "
n_tile=`(awk -F '.' '{print $1}')` //Will remove .png" without any messages.
# 5  
Old 05-17-2012
Hmm... renaming the files around at every turn, that seems awkward...
How about you create one textfile, that will have a look-up table defining the mapping (x,y) --> png, something like:
Code:
-10 -5 pic1.png
-10 -4 pic2.png
-10 -3 pic1.png
...
 10 5 pic28.png

And then you'd update this file accordingly. This way you wouldn't be dependent on the filename of pngs, and you don't need to have redundant copies of pngs if two tiles have the same image, and other advantages.
# 6  
Old 05-17-2012
Quote:
Originally Posted by Chubler_XL
Try this (remove echo when your confident it's going to do what you want):

Code:
#!/bin/sh
v1c=5
v2c=-5
cd ./tiles
for tile in *
do
  echo mv $tile `echo $tile | awk -vv1=$v1c -vv2=$v2c -F[_.] '{print $1 "_" $2+v1 "_" $3+v2 ".png" }'`
done

Thanks a lot!
It seems subtractions does not work though, but other than that it works like a charm.

Edit: Sorry about that, accidently added a "-" instead of a "+".

Last edited by Ravenholdt; 05-17-2012 at 09:20 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Renaming file that has multiple numbers as filename

Hi I have a file with filename as "partition-setup-and-ipl.vtcmd.76217657132.9721536798" Now i need to move this file as "partition-setup-and-ipl.vtcmd.76217657132.9721536798_org" i tried with # ls | grep -E "partition-setup-and-ipl.vtcmd.+"... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

2. Fedora

How to increase/Decrease Local Virtual consoles?

I m tring to figure out the option where i can increase/Decrease the count for local virtual consoles(vtys). I can able to take upto 6 vtys using (left alt+(F1-F6)) But is there any way i can increase the count ? One more query even though i can only take tty upto tty6 why there are tty... (2 Replies)
Discussion started by: pinga123
2 Replies

3. Shell Programming and Scripting

Awk : find progressive increase in numbers

NR_037575 -0.155613339079513 -0.952655362767482 -1.42096466949375 -0.797042023687969 -1.26535133041424 -0.468309306726272 NR_037576 0.59124585320226 0.408702582537126 0.888885242203586 -0.182543270665134 0.297639389001326 0.480182659666459... (4 Replies)
Discussion started by: quincyjones
4 Replies

4. Solaris

How to increase or decrease inode number of the particular UFS filesystem

Hi Gurus I want to know the command & tips regarding, how to increase or decrease inode number of the particular ufs filesystem. Is it possible to do it in a live/production environment. Regards (3 Replies)
Discussion started by: girish.batra
3 Replies

5. Shell Programming and Scripting

How to remove numbers from filename

Hi all, Can I edit this script: find . -type f | while read i;do && mv "$i" "${i//abc/}" ;done so that it will not only take out abc from the filename but also take out any numbers that might be in the filename as well. An example would be, Input: filename abc 2009.mov Output:... (7 Replies)
Discussion started by: Monkey Dean
7 Replies

6. Solaris

How to increase Inode numbers in Solaris 10

Hi guys, need your help on this since i dont know much about solaris. the problem is i need to increase inodes space on /export/home/ root@BRF-DANCCM1 # /usr/ucb/df -i Filesystem iused ifree %iused Mounted on /dev/vx/dsk/bootdg/rootvol 53026 1162206 ... (7 Replies)
Discussion started by: ichiko
7 Replies

7. Shell Programming and Scripting

delete numbers in a filename

I have some files where numbers are part of like eg 1add1.txt 23sub41.txt etc I want to remove numbers from the filenames(whereever it may be). I used echo `ls *.txt | sed -e "s///"` But its removing first digits like 1add1.txt becomes add1.txt My intention is to make 1add1.txt... (3 Replies)
Discussion started by: villain41
3 Replies

8. Shell Programming and Scripting

rename multiple filename.45267.txt to >> filename.txt

i have several thousand files and in subdirs that are named file.46634.txt budget.75346.pdf etc i want to remove the number but retain the extension. it is always a 5 digit. thanks. (6 Replies)
Discussion started by: jason7
6 Replies

9. Solaris

increase/decrease filesystem

Hi All, I need to increase the filesystem of / and /var (two different slices)? Space will be coming from /home slice so I need to decrease it. Is that possible without reinstallation or in a single-user-mode? Any idea or link please. Thanks in advance. (5 Replies)
Discussion started by: itik
5 Replies

10. Shell Programming and Scripting

script to change filename with numbers

ok, this one is definitely too hard for my shell-script-skills. Hopefully, there is somebody who can help me with this: I have a folder with files in it named 0.ppm 10.ppm 2.ppm ... 5.ppm 50.ppm 55.ppm ... 355.ppm 360.ppm etc. As you will notice, the order in which the files are... (5 Replies)
Discussion started by: silversurfer202
5 Replies
Login or Register to Ask a Question