Page 1 of 1
need help a tough to define problem.
Posted: Thu Aug 12, 2010 4:46 pm
by Zacariaz
Sorry to be bothering you with this tedious task, but I'm kinda in a hurry and I just can't figure out the solution.
I have a lot of files (I mean thousands) of the form "somename_X_Y.png" ex. mypicture_3_12.png
I need to increase the starting value of the x and y values, fx. if I want to start at 4, 14 in stead of 1, 1
mypicture_1_1.png
mypicture_1_2.png
mypicture_2_1.png
mypicture_2_2.png
would become:
mypicture_4_14.png
mypicture_4_15.png
mypicture_5_14.png
mypicture_5_15.png
This is something that I should be able to do, but I guess I've forgotten how and I haven't been able to find any answer anywhere.
I have both windows and linux so no need to worry bout that. I guess linux is the answer, but as I said, I've forgotten how
Anyway, I hope you'll help me out here.
best regards.
Re: need help a tough to define problem.
Posted: Fri Aug 13, 2010 12:45 am
by Solar
Generic solution finder advice (for Unix shells):
- Set up a testbed (Lines 501 to 506.)
- Start with a loop that somehow pics up all the files you want to be working on. Make sure you get the right files. (Line 507.)
- Start working on getting the information you want to be working on (here: the X and Y numbers), piecemeal, and check each of your steps. (Lines 508 to 511. Sorry for the missing closing parenthesis in line 509.)
- Use the information you gathered to assemble the command you want to execute. (Line 512.)
- Check your result. (Line 513.)
- Call 'history' to paste your solution path and brag a bit about your work. (Line 514, this post.)
The solution is the 'expr' command, which can do basic maths for you. Remember that, if you use the '*' operator, you have to escape it ('\*') or the shell will try to expand it into a list of files in your local directory.
Code: Select all
$ > history
501 export START_X=4
502 export START_Y=14
503 touch mypic_1_1.png
504 touch mypic_1_2.png
505 touch mypic_2_1.png
506 touch mypic_2_2.png
507 for file in mypic_*; do echo $file; done
508 for file in mypic_*; do X=$(echo $file | sed -e "s/mypic_//" -e "s/_.*//"); echo $X; done
509 for file in mypic_*; do X=$(echo $file | sed -e "s/mypic_//" -e "s/_.*//"); Y=$(echo $file | sed -e "s/.*_//" -e "s/.png//"; echo $Y; done
510 for file in mypic_*; do X=$(echo $file | sed -e "s/mypic_//" -e "s/_.*//"); Y=$(echo $file | sed -e "s/.*_//" -e "s/.png//"); echo $Y; done
511 for file in mypic_*; do X=$(echo $file | sed -e "s/mypic_//" -e "s/_.*//"); Y=$(echo $file | sed -e "s/.*_//" -e "s/.png//"); echo $(expr $X + $START_X); echo $(expr $Y + $START_Y); done
512 for file in mypic_*; do X=$(echo $file | sed -e "s/mypic_//" -e "s/_.*//"); Y=$(echo $file | sed -e "s/.*_//" -e "s/.png//"); mv $file mypic_$(expr $X + $START_X)_$(expr $Y + $START_Y).png; done
513 ls
514 history
PS: If the base name of the pictures is not consistent, you have to retrieve the base name from the filename. Depending on how exactly your file names are formed, that'd be something along these lines:
Code: Select all
echo $file | sed "s/\([^_]*\)_.*/\1/"
("Get a group of characters, which do not include an underscore. Then get remaining characters from that underscore to the end of the string. Replace with group (1) of characters found.")
Re: need help a tough to define problem.
Posted: Fri Aug 13, 2010 1:19 am
by Candy
You sed it Solar!
Re: need help a tough to define problem.
Posted: Fri Aug 13, 2010 1:43 am
by Zacariaz
wow, it has been a long time, but I'm sure I'll figure it out.
Thanks a bunch.