[Gllug] Processing filenames containing " with sed

Mike Brodbelt mike at coruscant.demon.co.uk
Thu Jun 3 23:04:03 UTC 2004


On Thu, 2004-06-03 at 17:19, Dylan wrote:
> Hi All,
> 
> I've been playing with Mike's oneliner:
> 
> /bin/ls -C1 | sed "s/\(.*\) - \(.*\).mp3/mv \"&\" \"\2 - \1.mp3\"/"|sh
> 
> (see Swapping two parts of a filename)
> 
> I have one problem - it chokes on any filename containing a double 
> quote. Since most of these mp3's are 12" versions, that's quite common! 
> Can I deal with this case as well? Remembering that there are filenames 
> with single quotes in them too!

You can stick another sed in the pipeline to escape the quotes. Like
so:-

$ /bin/ls -C1 |
> sed s/\"/\\\\\"/ |
> sed "s/\(.*\) - \(.*\).mp3/mv \"&\" \"\2 - \1.mp3\"/" |
> sh

If you want to do anything uglier, you should probably use perl :-).

Mike.

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




More information about the GLLUG mailing list