need help a tough to define problem.

All off topic discussions go here. Everything from the funny thing your cat did to your favorite tv shows. Non-programming computer questions are ok too.
Post Reply
User avatar
Zacariaz
Member
Member
Posts: 1069
Joined: Tue May 22, 2007 2:36 pm
Contact:

need help a tough to define problem.

Post 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 :roll:

Anyway, I hope you'll help me out here.


best regards.
This was supposed to be a cool signature...
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: need help a tough to define problem.

Post 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.")
Every good solution is obvious once you've found it.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re: need help a tough to define problem.

Post by Candy »

You sed it Solar!
User avatar
Zacariaz
Member
Member
Posts: 1069
Joined: Tue May 22, 2007 2:36 pm
Contact:

Re: need help a tough to define problem.

Post by Zacariaz »

wow, it has been a long time, but I'm sure I'll figure it out.

Thanks a bunch.
This was supposed to be a cool signature...
Post Reply