I love Perl

If you ever learn one line of Perl, learn this. It’s so useful. I used this to fix a playlist which got messed up when I changed the server that holds my network drive with all my music on it. I first exported the playlist as an .xspf file which had lines like this in it:

?:\Music\00 - Ambient...

you see at the beginning of the path the ?: which is not right. My network drive is at M: so it should look like this:

M:\Music\00 - Ambient

this is really easy to change with Perl:

perl -pe 's/\?/M/g' my_playlist.xspf

you will notice that I had to escape the ? above because it is one of the special control characters which must be escaped inside a regular expression when it occurs outside a character class: .^$*+?()[{\|

To view the output of the command above just pipe it to the pager less:

perl -pe 's/\?/M/g' my_playlist.xspf |less

once you’ve examined it and are sure you have your regex substitution working correctly, redirect the output to a file:

perl -pe 's/\?/M/g' my_playlist.xspf > my_playlist-new.xspf

If you have no idea what any of this is or what I’m talking about: This is stuff you do in a command shell. If you’re on Windows you would want to install Cygwin and a nice console app like ConEMU. On Mac you already have a console built in, same with Linux.

Here’s a good reference on Perl regular expressions.


Comments

Leave a Reply