By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,276 Members | 1,430 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,276 IT Pros & Developers. It's quick & easy.

Parse multiple files...

P: 3
I feel like a goon. I had a script that would do this but I deleted it long ago without even thinking. Please if someone could help I would REALLY appreciate it.

I'm looking for a script that will read all of the files in a directory and pull the data between two sets of tags and put it in a tab delimited file. Such as, find:

<span class."inv">(.+?)<\/span>
<td class."lister">(.+?)<\/td>

Then write to a new file: $1 \t $2 \n (not sure if that's right)

If you're sitting on a script that does something similar I can probably edit it for my use, I just can't flat-out write it yet. Please and thank you!
Mar 18 '08 #1
Share this Question
Share on Google+
2 Replies

Expert 100+
P: 971
Please try and code it yourself and if you get stuck the we will be glad to assist you. Look into using the HTML::Parser.

Mar 18 '08 #2

P: 3
Expand|Select|Wrap|Line Numbers
  1. $dir = "c:\\script";
  2. $outdir = "c:\\newfiles";
  4. ### DO NOT EDIT BELOW ###
  5. $i=1;
  6. print "Opening $dir\n";
  7. opendir(DH,$dir);
  8. while (defined ( my $filename = readdir(DH))) {
  9.     $outfilename=">\\$outdir\\$filename";
  10.     print "Opening $filename\n";
  11.     open(FHI,$filename);
  12.     while (<FHI>) {
  13.         $html .= $_;
  14.     }
  15.     close(FHI);
  17.     while ($html =~ s/<span2>(.+?)<\/span2>/$1/) {
  18.     $i++;
  19.     }
  20.     print "$i matches found in $filename\n";
  21.     print "Saving to $outfilename\n";
  22.     open(FHO, $outfilename);
  23.     print FHO ($html);
  24.     close(FHO);
  25. }
  26. print "Done\n";
Problem is it tries to open everything (even the . and .., lol), claims to have found the tags in everything, claims to save it to another file, but has succeeded in none of it.
Mar 19 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.