|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Hello,
I'm good with Unix, but I haven't learned Perl yet.... I know Perl can do this, but since I don't know Perl yet, I'm a little hung up. Problem: I need to rename a batch of files: foo_001.jpg foo_002.jpg foo_003.jpg bar_001.jpg bar_002.jpg bar_003.jpg .....and so on. I need to take the number on these files (notice how the number is duplicated on foo and bar) and make the entire batch consecutive. In other words, I want the file list to look like this: foo_001.jpg foo_002.jpg foo_003.jpg bar_004.jpg bar_005.jpg bar_006.jpg .....and so on. I'd appreciate any ideas! I just feel like this is so simple and I'm just not seeing the answer! Thanks - Peter |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
In article <1188589094.698861.37200@i38g2000prf.googlegroups. com>,
gibbonsp <gibbonsp@gmail.com> wrote: (From) >foo_001.jpg >foo_002.jpg >foo_003.jpg >bar_001.jpg >bar_002.jpg >bar_003.jpg >....and so on. (To) >foo_001.jpg >foo_002.jpg >foo_003.jpg >bar_004.jpg >bar_005.jpg >bar_006.jpg >....and so on. gawk -F_ '{ print "mv -iv",$0,$1"_"sprintf("%03d.jpg",++n) }' file1 > /tmp/dofile Then bring up /tmp/dofile in your editor, check it over, and when you are happy with it (and you've confirmed that "mv -iv" behaves the same way on your system as it does on mine), source the file from your shell. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
2007-08-31, 19:38(-00), gibbonsp:
> Hello, > > I'm good with Unix, but I haven't learned Perl yet.... I know Perl can > do this, but since I don't know Perl yet, I'm a little hung up. > > Problem: > > I need to rename a batch of files: > > foo_001.jpg > foo_002.jpg > foo_003.jpg > bar_001.jpg > bar_002.jpg > bar_003.jpg > ....and so on. > > I need to take the number on these files (notice how the number is > duplicated on foo and bar) and make the entire batch consecutive. In > other words, I want the file list to look like this: > > foo_001.jpg > foo_002.jpg > foo_003.jpg > bar_004.jpg > bar_005.jpg > bar_006.jpg > ....and so on. [...] zsh (assuming autoload -U zmv in ~/.zshrc): n=0; zmv -n '(*_)<->(*)' '$1${(l:3::0 $((++n))}$2'recursively: n=0; zmv -n '(**/)(*_)<->(*)' '$1$2${(l:3::0 $((++n))}$3'Remove "-n" when happy. -- Stéphane |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
In article <1188589094.698861.37200@i38g2000prf.googlegroups. com>,
gibbonsp <gibbonsp@gmail.com> wrote: >I need to rename a batch of files: > >foo_001.jpg >foo_002.jpg >foo_003.jpg >bar_001.jpg >bar_002.jpg >bar_003.jpg >....and so on. > >I need to take the number on these files (notice how the number is >duplicated on foo and bar) and make the entire batch consecutive. In >other words, I want the file list to look like this: > >foo_001.jpg >foo_002.jpg >foo_003.jpg >bar_004.jpg >bar_005.jpg >bar_006.jpg >....and so on. > >I'd appreciate any ideas! I just feel like this is so simple and I'm >just not seeing the answer! A ksh93 utility that has various options for just this sort of thing: ftp://ftp.armory.com/pub/scripts/ren In this case, it would be: ren -m2=s -z3 '*_*.jpg' ("replace the second globbed segment with sequential integers that contain 3 digits") John -- John DuBois spcecdt@armory.com KC6QKZ/AE http://www.armory.com/~spcecdt/ |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
In article <1188589094.698861.37200@i38g2000prf.googlegroups. com>,
gibbonsp <gibbonsp@gmail.com> wrote: >I need to rename a batch of files: > >foo_001.jpg >foo_002.jpg >foo_003.jpg >bar_001.jpg >bar_002.jpg >bar_003.jpg >....and so on. > >I need to take the number on these files (notice how the number is >duplicated on foo and bar) and make the entire batch consecutive. In >other words, I want the file list to look like this: > >foo_001.jpg >foo_002.jpg >foo_003.jpg >bar_004.jpg >bar_005.jpg >bar_006.jpg >....and so on. > >I'd appreciate any ideas! I just feel like this is so simple and I'm >just not seeing the answer! A ksh93 utility that has various options for just this sort of thing: ftp://ftp.armory.com/pub/scripts/ren In this case, it would be: ren -m2=s -z3 '*_*.jpg' ("replace the second globbed segment with sequential integers that contain 3 digits") John -- John DuBois spcecdt@armory.com KC6QKZ/AE http://www.armory.com/~spcecdt/ |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
On Sep 2, 1:33 am, spce...@armory.com (John DuBois) wrote:
> In article <1188589094.698861.37...@i38g2000prf.googlegroups. com>, > > > > gibbonsp <gibbo...@gmail.com> wrote: > >I need to rename a batch of files: > > >foo_001.jpg > >foo_002.jpg > >foo_003.jpg > >bar_001.jpg > >bar_002.jpg > >bar_003.jpg > >....and so on. > > >I need to take the number on these files (notice how the number is > >duplicated on foo and bar) and make the entire batch consecutive. In > >other words, I want the file list to look like this: > > >foo_001.jpg > >foo_002.jpg > >foo_003.jpg > >bar_004.jpg > >bar_005.jpg > >bar_006.jpg > >....and so on. > > >I'd appreciate any ideas! I just feel like this is so simple and I'm > >just not seeing the answer! Assuming that your filenames follow the pattern .*_¥d{3}.jpg #!/bin/bash #dir files contains all the files files=`ls files` i=1 padding="000" for file in $files do ((len=3-${#i})) j="${padding:0:${len}}${i}" newName=${file/_???./_${j}.} mv files/${file} files/${newName} ((i=i+1)) done |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
arch119 wrote:
> > Assuming that your filenames follow the pattern .*_¥d{3}.jpg > > > #!/bin/bash > > [...] > i=1 > padding="000" > [...] > ((len=3-${#i})) > j="${padding:0:${len}}${i}" > [...] If you want leading zeros: $ i=7 $ printf -v j "%03d" $i $ echo $j 007 -- Best regards | "The only way to really learn scripting is to write Cyrus | scripts." -- Advanced Bash-Scripting Guide |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
Hello,
arch119 wrote: > > #!/bin/bash > > #dir files contains all the files > files=`ls files` With output of "ls" you get problems with spaces and line breaks in filenames. > i=1 > padding="000" > for file in $files better: for file in * > do > ((len=3-${#i})) > j="${padding:0:${len}}${i}" > newName=${file/_???./_${j}.} > mv files/${file} files/${newName} > ((i=i+1)) > done If you want leading zeros: $ i=7 $ printf -v j "%03d" $i $ echo $j 007 -- Best regards | "The only way to really learn scripting is to write Cyrus | scripts." -- Advanced Bash-Scripting Guide |
|
![]() |
| Outils de la discussion | |
|
|