4 ms·
Here are a few quick and dirty I use for handling output of MySQL. (For serious stuff I include real CSV libraries in the one liner to quote correctly etc.)
by berntb 10y ago
Here are a few quick and dirty I use for handling output of MySQL. (For serious stuff I include real CSV libraries in the one liner to quote correctly etc.)
alias sqlcmdtocsv="perl -nE 'chomp; s/\\t/;/g; say \$_;'"
alias sqlcmdtoperl="perl -MData::Dump=dump -nE 'chomp; @r=split(/\\t/); if (@titles == 0) { @titles=@r; next; } \$row={}; for(\$i=0; \$i < @r; \$i++) { \$row->{\$titles[\$i]} = \$r[\$i]; } push @rows, \$row; END { say dump \\@rows };'"
# Quick and dirty for moving from the real DB to the test DB.
# Use: mysql_generates_a_row | sqlcmdtoperl | dumptosqlinsert
alias dumptosqlinsert="perl -E 'my \$txt; { local \$/; \$txt=<>; } my \$rows= eval \$txt; \$q=chr(39); for my \$r (@\$rows) { @cols=map { \$v=\$r->{\$_}; if (\$v eq \"NULL\") {\"\$_=NULL\"} else {\"\$_=\${q}\$v\$q\"} } keys %\$r; say join(\", \", @cols);}'"
I have a bunch of aliases for conversions, to generate common SQL that dependend on parameters and so on.
- pre_action 10y agoI wrote the `ysql` Perl application for exactly this problem
- berntb 10y agoIt was faster than Googling. :-) But it would have been better to build off a well tested module as a basis for my set of tools, sigh. I could have used yaml as an intermediary. A favorite saying is from chemistry -- with a few weeks of hard work in the lab, you can save hours in the library...