[Gllug] Swapping two parts of a filename...

Mike Brodbelt mike at coruscant.demon.co.uk
Wed Jun 2 23:34:33 UTC 2004


On Wed, 2004-06-02 at 23:40, Dylan wrote:

> > $ touch "Track Name - Artist Name.mp3"
> > $ ls
> > Track Name - Artist Name.mp3
> > $ /bin/ls -C1 | sed "s/\(.*\) - \(.*\).mp3/mv \"&\" \"\2 -
> > \1.mp3\"/"|sh $ ls
> > Artist Name - Track Name.mp3
> >
> >
> > Adjust your regex to fit...
> 
> Well, I'll trust it does what you say (will try it on a test file 
> first!) but it looks like gobledygook to me so adjusting the expression 
> to fit will be fun!

As posted it should deal with the situations you mentioned. To go over
how it works briefly, we pipe the output of a single column ls listing
to sed, and the use that to do a substitution. We look for a pnatter to
match '\(.*\) - \(.*\).mp3', which basically grabs the entire line for
anything thet ends in .mp3 and contains " - ". The bits before and after
those three characters are matched with the .* atoms, which are captured
by sticking them in parentheses. We then replace with the whole match
(i.e. the filename) with 'mv \"&\" \"\2 \1.mp3\"'. The & gets replaces
by the entire match, and \1 and \2 refer to the captured bits. So, the
sed bit basically contructs a list of mv commands. Then that just gets
piped into a shell to make it happen. The only bit you'd probably want
to tweak is the " - " bit in the first bit - that's basically the
separator string.

Mike.

-- 
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug




More information about the GLLUG mailing list