MLUG: Re: [MLUG] Renaming a bunch of files
Re: [MLUG] Renaming a bunch of files
Email address obfuscation in effect -- please click here to turn it off.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
On Thu, 1 Mar 2001, Rob Judd wrote:

> > -----------------------------------------------------------
> > #!/usr/bin/bash1 -f
> > while read file; do
> >   mv "$file" "`echo $file | awk -F: '{print $NF}'`"
> > done < ../songlist
> > -----------------------------------------------------------
> 
> Okay, I can't resist - the construct you were looking for was:
> 
> for file in *; do
>  mv "$file" "`echo $file | awk -F: '{print $NF}'`"
> done

This is a perl one-liner I stuck in a file for readability.  Advantages
are that it only fires up perl itself and then one mv per file rather than
one mv, one echo, one awk, and quoting hell.

#!/usr/bin/perl -wln

s/ /\\ /g;           # quote spaces
my @F = split(/:/);  # split on fields separated by ":"
`mv $_ @{[pop @F]}`; # do the move, with one hand juggling.
                     # non-jugglers can do:
                     #  my $dest = pop @F; `mv $_ $dest`;

The -w switch to perl should always be used.  
The -l switch in this case does an automatic "chomp" on each line of 
       input.
The -n puts an automatic read-no-print loop around your code.
 
Annoyingly, your destination file names have spaces in them.  Otherwise,
I could reduce this to:

#!/usr/bin/perl -wlanF:

s/ /\\ /g;           # quote spaces
`mv $_ @{[pop @F]}`; # do the move, with one hand juggling.
                     # non-jugglers can do:
                     #  my $dest = pop @F; `mv $_ $dest`;

The -a switch enables (wait for it...) awk-mode; fields are in @F.
The -F switch changes the field separator, in this case to ":"

This would now be a legitimite one-liner:

perl -wlanF: -e 's/ /\\ /g and `mv $_ @{[pop @F]}`'

But, again, that won't work in your case. :-(

> That doesn't have the problem of breaking up the filenames on spaces.  I
> can explain if you really want ... it's to do with which program sees the
> arguments and the order they are processed in.

I grow old...I grow old.  I cannot be bothered with a lot of this stuff
anymore. :-)
 
jking


--
To manage your subscription, go to http://mlug.missouri.edu/members/edit.php

Archives are available at http://mlug.missouri.edu/list-archives/