Posts

Showing posts from July, 2009

Rip from WAV to MP3 and add ID3 tags -- All with Perl

Ok - so I know I have said before that I am not much of a perl fan. ( yes it's safe, I am wearing my flame proof suit. ) I will, however, say now; I am coming around. It would seem that I am starting to see the light. I needed to extract some files from a CD I have and convert them to MP3. The CD was not made with extracting to mp3 in mind if you get my drift. It's ok, because the CD is my copy and I want to listen to it on my iPod. Regular extracting on iTunes was not working out for me. So I rolled my chair over to my trusty Linux Box and perl, id3, cdparanoia and lame. First thing I did was use cdparanoia to extract all the tracks. This went swimmingly leaving me with a folder full of files called 01.cdparanoia.wav or something just as useless. The files played ok but I needed to name them all. This particular CD was not listed on Music Brainz so I couldn't do anything fancy with FreeCDDB or whatever they call that now days. * shudder * - I had to manually edit

Real simple to connect to MSSQL from Perl

Again, this took me a little while but here I am. Connecting to a database ( SQL SERVER 2008 ) from a Windows Server 2008 VM I own ( thanks TECH ED 2008 ) and Active State Perl. Install Scite cos it's a nice editor and you are away laughing... #!/usr/bin/perl use DBI; my $DSN = 'driver={SQL Server};Server=localhost; database=AdventureWorks;TrustedConnection=Yes'; my $dbh = DBI->connect("dbi:ODBC:$DSN") or die "$DBI::errstr\n"; my $sth = $dbh->prepare('select top 10 * from Sales.vSalesPerson') or die "Couldn't prepare statement: " . $dbh->errstr; $sth->execute(); while( @data = $sth->fetchrow_array()) { foreach(@data) { print "[$_]"; } print "\n\n"; } $sth->finish; $dbh->disconnect; Results: C:\Users\dave\Documents>perl perl1.pl [268][][Stephen][Y][Jiang][][North American Sales Manager][238-555-0197][stephen 0@adventure-works.com][0][2427 Notre Dame Ave.][][Redmond]

Basic Perl file parser - useful for working with logs

Really I just need to remember how to do this: #!/usr/bin/env perl #yum log file use warnings; use strict; open FILE, "/var/log/yum.log" or die $!; while (<FILE>) { /^(.+?)\s(.+?)\s(.+?)\s(.+?)\s(.+?)\s/; my ($month,$day,$time,$action,$package) = ($1,$2,$3,$4,$5); if( $action =~ /Installed/ ) { my $suffix = 'th'; if( $day == 1 ){ $suffix = 'st'; } if( $day == 21 ){ $suffix = 'st'; } if( $day == 31 ){ $suffix = 'st'; } if( $day == 2 ){ $suffix = 'nd'; } if( $day == 22 ){ $suffix = 'nd'; } if( $day == 3 ){ $suffix = 'rd'; } if( $day == 23 ){ $suffix = 'rd'; } print "$day$suffix of $month at $time\n" } } close FILE

Joining multiple PDF documents with Gostscript.

This really works. I downloaded all the chapters of beginning perl as published here: http://www.perl.org/books/beginning-perl/ To do it in a painless way I first used curl to grab the html. curl http://www.perl.org/books/beginning-perl/ -o bp.txt Then I used grep to grab the hyperlinks to pdf files and piped that into another file. cat bp.txt | grep -o "<a.*pdf"> bp2.txt Then I used sed to clean up a little bit. cat bp2.txt | sed "s/^<a href=\"//g"> bp3.txt Then I used wget to download each pdf. for line in `cat bp3.txt`; do wget $line; done So now I had a whole bunch of pdf files that together make up one book. A quick hunt on google produced this guy. http://www.linux.com/news/software/applications/8229-putting-together-pdf-files And I was away laughing. gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=beginning-perl.pdf 3145_Intro.pdf 3145_Chap*.pdf 3145_App*.pdf 3145_Index.pdf

Samba File Sharing to Windows ( Includes Vista )

I found this: http://www.mjmwired.net/resources/mjm-fedora-f11.html#samba Here are the main points: Install Samba yum install samba samba-client Configure Samba ( as root ) # vim /etc/smb.conf Add a workgroup name under the [globals] section. Add your fileshares at the end of the file. [TerraByte] path = /media/TerraByte public = yes writable = yes This will create a writable share. File permissions in linux must match. Ie: (dwrx-dwr-dwr ). If your drive is an NTFS formatted drive, these permissions can not be applied. It doesn't matter though because it still works. Set up samba users. Users must already exist as users on your linux system. They must be valid users. The password does not need to match. There are a bunch of warnings that can be ignored. # smbpasswd -a username New SMB password: Retype new SMB password: account_policy_get: (warnings ignored) Added user username. Start the samba service and enable it in chkconfig levels 3 and 5. Enable samba in the fi