nix, shell, perl, php, mysql and mac os x tips and tricks

Friday, September 19, 2014

Perl subroutine to fix when html entities get recursively applied

sub enEntity {
 
 my $string = shift;
 
 if ($string =~ /&/) {
  #print "string: $string\n";
 }
 if ($string =~ /&(amp;)+quot;/) { $string =~ s/&(amp;)+quot;/"/ig; }
 if ($string =~ /"/) { $string =~ s/"/"/ig; } 
 if ($string =~ /"/) { $string =~ s/"/"/ig; } 
 if ($string =~ /&(amp;)+apos;/) { $string =~ s/&(amp;)+apos;/'/ig; }
 if ($string =~ /'/) { $string =~ s/'/'/ig; } 
 if ($string =~ /'/) { $string =~ s/'/'/ig; } 
 if ($string =~ /&(amp;)+/) { $string =~ s/&(amp;)+/&/ig; }
 if ($string =~ /&/) { $string =~ s/&/&/ig; }
 if ($string =~ /&/) { $string =~ s/&/&/ig; }
 #optional
        #$string = unidecode($string);

 return ($string);

} # end sub unescape html entities

Convert raw (batch) mysql command-line output to CSV

mysql --user=wibble --password wobble -B -e "select * from vehicle_categories;" | sed "s/'/\'/;s/\t/\",\"/g;s/^/\"/;s/$/\"/;s/\n//g" > vehicle_categories.csv