On Mon, Oct 17, 2011 at 9:07 PM, Csanyi Pal <csanyi...@gmail.com> wrote:
> Leo Susanto <leosusa...@gmail.com> writes: > > > On Mon, Oct 17, 2011 at 11:46 AM, Csanyi Pal <csanyi...@gmail.com> > wrote: > >> my goal is to get in a directory the following: > >> > >> PIC00001.JPG renamed as Gyermekolimpia_Ujvidek_001.jpg > >> PIC00002.JPG renamed as Gyermekolimpia_Ujvidek_002.jpg > >> ... > >> PIC00223.JPG renamed as Gyermekolimpia_Ujvidek_223.jpg > >> > >> > >> So far I get this to work: > >> > >> rename -n 's/\w\w\w\w\w/sprintf("Gyermekolimpia_Ujvidek_")/e' *.JPG > >> PIC00001.JPG renamed as Gyermekolimpia_Ujvidek_110.JPG > >> etc. > >> PIC00223.JPG renamed as Gyermekolimpia_Ujvidek_223.JPG > >> > >> This is good but I want to make the .JPG extensions lowercase. > >> > >> I know that the following command do this: > >> rename -v 's/\.JPG$/\.jpg/' *.JPG > >> > >> but I want to know how to add > >> \.JPG$/\.jpg/ > >> > >> into the following command? > >> rename -n 's/\w\w\w\w\w/sprintf("Gyermekolimpia_Ujvidek_")/e' *.JPG > > > s/.+(\d{3}).jpg/Gyermekolimpia_Ujvidek_$1.jpg/i > > rename -v 's/.+(\d{3}).jpg/Gyermekolimpia_Ujvidek_$1.jpg/i' *.JPG > > It works perfectly. Can you explain to me how does it works? > > -- > Regards, Pal > <http://cspl.me> > > > -- > To unsubscribe, e-mail: beginners-unsubscr...@perl.org > For additional commands, e-mail: beginners-h...@perl.org > http://learn.perl.org/ > > > I'll try :-) Lets split it in the various parts first: .+ (\d{3}) . (this is not completely correct but we will get to that) jpg The first part basically tells the regular expression engine to find '.' (any character) and to find it as often as it can + The second step finds \d (a digit) exactly 3 times {3} and it is in brackets which means these digits are captured for reuse later (we will get to that as well). Thanks to the fact that this capture group is here the above .+ will not simply capture all characters but will stop due to the fact that this bit matches the 3 digits in the file name. The third bit where I said it is wrong is the . which captures any one character after the 3 digits, it should probably have read \. which restricts it to the . alone. Then the last bit is no more then the extension preventing this from matching some_file123.txt etc... The next part is where we deal with the replacing of the previous file name with the new one. The first bit is no more then the new file name: Gyermekolimpia_Ujvidek_ Then the $1 which stands for the first capture group or (\d{3}) in our case is placed here The last bit places the extension there as .jpg Then the one trick in all of this is the i at the end of the regular expression which tells it to work in a case insensitive mode, so the capture portion of your substitution that finds all files that match will find all files where the last 3 charters are jpg regardless of how these are written. So jpg or JPG or any other combination like JpG will be found providing that the regular expression engine gets this far, it will stop trying to match as soon as any of the elements above fails. Regards, Rob