News for hackers. Stuff that matters.
Re:Useful Perl one-liners (2013) by berntb on Tuesday November 29, 2016 @01:28PM (#13065030)
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.
Re:Useful Perl one-liners (2013) by pre_action on Tuesday November 29, 2016 @06:40PM (#13067207)
I wrote the `ysql` Perl application for exactly this problem
Re:Useful Perl one-liners (2013) by berntb on Wednesday November 30, 2016 @07:24AM (#13070118)
It 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...