User Name
Password

Go Back   Planetarion Forums > Non Planetarion Discussions > Programming and Discussion
Register FAQ Members List Calendar Arcade Today's Posts

Reply
Thread Tools Display Modes
Unread 16 Jan 2003, 21:11   #1
Intervention
In fond memory of SB
 
Join Date: Dec 2002
Location: LIGHTNING BOLT!!
Posts: 393
Intervention is an unknown quantity at this point
File / File & String Processing Question (PHP or Perl)

I have a file which contains things like this:

Code:
[G:\Judas Priest - Turbo]
01 Turbo Lover.mp3=5334541
02 Locked In.mp3=4154224
03 Private Property.mp3=4328513
04 Parental Guidance.mp3=3308274
05 Rock You All Round The World.mp3=3477548
06 Out In The Cold.mp3=6202224
07 Wilds, Hot Crazy Days.mp3=4482322
08 Hot For Love.mp3=4033852
09 Reckless.mp3=4161329

[G:\King Crimson - Beat]
Heartbeat.mp3=3747954
Neal And Jack And Me.mp3=4196636
Neurotica.mp3=4609476
Requiem.mp3=6294994
Sartori In Tangier.mp3=3439804
The Howler.mp3=4033178
Two Hands.mp3=3249220
Waiting Man.mp3=4218750
What I'm wanting to do is to process them so that they are something like this:

Code:
Judas Priest:Turbo:Turbo Lover
Judas Priest:Turbo:Locked In
Judas Priest:Turbo:Private Property
Judas Priest:Turbo:Parental Guidance
Judas Priest:Tur......

King Crimson:Beat:Heartbeat
King Crimson:Beat:Neal and Jack And Me
King Crimson:Beat:Nuerotica
etc.

I've heard Perl is good for this but I only have very minute experience with it.

If somebody could give an example or two on how this is done or some pointers on where to start looking about how to do it then I would greatly appreciate it.

I've done a few google searchs but the results seemed either too complicated or on the wrong sort of area that Iam wanting.


Thanks for any help.
__________________
Peace, Love and Linux

--------------
My Artwork
Intervention is offline   Reply With Quote
Unread 16 Jan 2003, 21:43   #2
zenopus
Xenoc
 
Join Date: Feb 2001
Location: Great Britain
Posts: 297
zenopus is an unknown quantity at this point
Something like this.
Code:
#!/usr/bin/perl
#
#

use strict;   # be picky about stuff
$^W = 1;      # enable warnings
$\ = "\n";    # stick newline at the end of what we print

my ($band,$album,$track,$size); # variables

while (<>) { # for each line
  chomp;      # trim the newline
  next if (/^\s*$/); # skip blank lines
  if (/^\[.:\\.+ - .+\]$/) { # new album
    ($band,$album) = /^\[.:\\(.+) - (.+)\]$/;
  }
  else {      # new track
    ($track,$size) = /[\d\s]*(.*)\.mp3=(\d+)/;
    print "$band:$album:$track";
  }
}
zenopus is offline   Reply With Quote
Unread 16 Jan 2003, 22:26   #3
Intervention
In fond memory of SB
 
Join Date: Dec 2002
Location: LIGHTNING BOLT!!
Posts: 393
Intervention is an unknown quantity at this point
Thanks, zenopus.

That'll give me a good start on things
__________________
Peace, Love and Linux

--------------
My Artwork
Intervention is offline   Reply With Quote
Unread 17 Jan 2003, 02:05   #4
Nodrog
Registered User
 
Join Date: Jun 2000
Posts: 8,476
Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.
Code:
#!/usr/bin/perl
my (@list, $filenames, $file);
opendir(DIR, "@ARGV[0]");      # open the directory
chdir("@ARGV[0]");
@filenames = readdir(DIR);      #get a list of all filenames
foreach (@filenames) {      #for every file
	$file = $_;      #take a note of file name
	if ($_ eq "." || $_ eq "..") {next;}      #skip this file if its . or ..
	($artist, $albumn) = /([^-]*)\s-\s([^\.]*)/;       #get the artist/albumn from file name
	open(FILE, "<$file");      #open this file
	while(<FILE> ) {     #for every song in this file
		push(@list,($artist . ":" . $albumn . ":"));      #add artist/albumn name to list
		/([a-zA-z][^\.]*)/; push(@list, ($1 . "\n"));      #extract song name and add it to list
	}
	close(FILE); 
	open(FILE, (">$file".".fixed.txt")); print FILE @list; close(FILE);      #write the list to a new file
	@list = ();     #empty the list (I suck at perl)
}
pass it the name of the directory containing the files, and it will make formatted files for every file in the directory. All the files should be called "Artist - Albumn.whatever", and the new files will be "Artist - Albumn.fixed.txt". If you want it to overwrite the original files, remove the ."fixed.txt" bit from the 2nd last line (check its actually outputting them in the style you want before you do this though)

edit - hmm, I suspect I read your post wrong... Is it one big file you have or lots of little ones?

Last edited by Nodrog; 17 Jan 2003 at 03:15.
Nodrog is offline   Reply With Quote
Unread 17 Jan 2003, 17:07   #5
Intervention
In fond memory of SB
 
Join Date: Dec 2002
Location: LIGHTNING BOLT!!
Posts: 393
Intervention is an unknown quantity at this point
The files contain multiple smaller listings like this

Sorry for not making it very clear
__________________
Peace, Love and Linux

--------------
My Artwork
Intervention is offline   Reply With Quote
Unread 17 Jan 2003, 17:13   #6
Mong
Forever Delayed
 
Join Date: Sep 2000
Location: www.netgamers.org
Posts: 1,475
Mong is on a distinguished road
Then it needs to say something like:

if part_of_filename = directory_name
{
delete offending_part_of_file_name
}

I guess a simple str_replace (dunno if it's the same in Perl):

str_replace ($dir_name, "", $filename);

M.
__________________
Firefly Oper and General l4m3r - "I Do Stuff"

O2 Rip-off campaign

<vampy> plus i hate people ... i despise humanity as a whole

pablissimo "I'm still geting over the fact you just posted a pic of your own vomit"
Mong is offline   Reply With Quote
Unread 17 Jan 2003, 21:20   #7
zenopus
Xenoc
 
Join Date: Feb 2001
Location: Great Britain
Posts: 297
zenopus is an unknown quantity at this point
The file at that link can be parsed with
Code:
#!/usr/bin/perl
#
#

use strict;   # be picky about stuff
$^W = 1;      # enable warnings
$\ = "\n";    # stick newline at the end of what we print
$, = ':';     # stick colons between things we print

my ($band,$album,$file,$type,$size); # variables
my @va; # array

while (<> ) { # for each line
  chomp;      # trim the newline
  next if (/^\[.:\]/); # skip disk entry
  next if (/^\s*$/); # skip blank lines
  if (/^\[.:\\.+ - .+\]$/) { # :\band - album
    ($band,$album) = /^\[.:\\(.+) - (.+)\]$/;
  }
  elsif (/^\[.:\\.+\]$/) { # :\album
    ($album) = /^\[.:\\(.+)\]$/;
  }
  else {      # new track
    @va = split /[\s_]-[\s_]/;
    shift @va if ($va[0] =~ /^\d+$/);  # skip track numbers
    $band = shift @va if (@va>1);      # entry with band name
    $_ = shift @va;                    # now get
    ($file,$type,$size) = /^(.*?)\.(.*?)=(\d+)$/ or next;
    print $band,$album,$file if ($type =~ "mp3");
  }
}
zenopus is offline   Reply With Quote
Unread 18 Jan 2003, 12:21   #8
Intervention
In fond memory of SB
 
Join Date: Dec 2002
Location: LIGHTNING BOLT!!
Posts: 393
Intervention is an unknown quantity at this point
Thanks zenopus and Nodrog. I'll experiment when I have more time later today.

Greatly appreciated.
__________________
Peace, Love and Linux

--------------
My Artwork
Intervention is offline   Reply With Quote
Reply



Forum Jump


All times are GMT +1. The time now is 05:11.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2002 - 2018