sorter.pl 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #!/usr/bin/env perl
  2. #
  3. # Fanboy Adblock Sorter v1.5 (22/03/2011)
  4. #
  5. # Dual License CCby3.0/GPLv2
  6. # http://creativecommons.org/licenses/by/3.0/
  7. # http://www.gnu.org/licenses/gpl-2.0.html
  8. #
  9. # Usage: perl sort.pl <filename.txt>
  10. #
  11. use strict;
  12. use warnings;
  13. use File::Copy;
  14. sub output {
  15. my( $lines, $fh ) = @_;
  16. return unless @$lines;
  17. print $fh shift @$lines; # print first line
  18. print $fh sort { lc $a cmp lc $b } @$lines; # print rest
  19. return;
  20. }
  21. # ======== Main ========
  22. #
  23. my $filename = shift or die 'filename!';
  24. my $outfn = "$filename.out";
  25. # die "output file $outfn already exists, aborting\n" if -e $outfn;
  26. # prereqs okay, set up input, output and sort buffer
  27. #
  28. open my $fh, '<', $filename or die "open $filename: $!";
  29. open my $fhout, '>', $outfn or die "open $outfn: $!";
  30. # Mark filenames for moving (overwriting..)
  31. #
  32. my $filetobecopied = $outfn;
  33. my $newfile = $filename;
  34. # Keep in Binary thus keep in Unix formatted text.
  35. #
  36. binmode($fhout);
  37. my $current = [];
  38. # Process data
  39. # Check "!", "[]" and "#" and ";" (Firefox and Opera)
  40. #
  41. while ( <$fh> ) {
  42. if ( m/^(?:[!\[]|[#|;]\s)/ ) {
  43. output $current, $fhout;
  44. $current = [ $_ ];
  45. }
  46. else {
  47. push @$current, $_;
  48. }
  49. }
  50. # Finish Up.
  51. #
  52. output $current, $fhout;
  53. close $fhout;
  54. close $fh;
  55. # Move backup file.out (sorted) overwrite orginal (non sorted)
  56. #
  57. move($filetobecopied, $newfile);