MySQLのselect結果をコマンドライン上でカンマ区切りとかに
mysqlのselectの結果をcsvで出したい場合。
よく、
SELECT * FROM table_name INTO OUTFILE "/tmp/sample.csv" FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"';
こんな事やってたんですが、結果が数行とかの時は、remote上からscpなんかでfile移動するのが面倒でした。
コマンドライン上でカンマ区切りとかに
mysql -uuser database -p -e 'SELECT id,a_id FROM test limit 10' -B | sed -e 's/\t/,/g' id,a_id 1,2 3,4 2,16 7,17 5,24 4,25 6,26 8,28 20,29
こんな感じで、コマンドライン上でカンマ区切りで出力。
コマンドライン上からSELECT文投げてます -B でbatch modeで出すので、tab区切りになります。 sedで、tab -> comma に置換してます。
特定の列の値のカンマ区切りが欲しい時とかは、
mysql -uuser database -p -e 'SELECT id FROM test limit 10' -B | tr '\n' ',' id,25,31,62,63,94,261,291,308,310,339
とか
jsonをphpでparseする際にコロンが含まれてた時の対応メモ
明らかに次もはまりそうなので、メモ
code
$rss_file = file_get_contents($rss_url); $rss_data_obj = json_decode($rss_file);
json_decodeした場合、stdClassで返却される。
object(stdClass)#22 (1) { ["feed"]=> object(stdClass)#23 (8) { ["author"]=> ...
stdClassなので、以下のように値を取ればいいのだが、
$rss_data_obj->feed->author;
困ったのはこういうパターン
["entry"]=> object(stdClass)#27 (12) { ["im:name"]=> object(stdClass)#28 (1) { ...
['im:name'] という名前だと、->im:name というようにオブジェクトとして呼び出せない。
しょうがないのでarrayにキャストする
参考: Object→Arrayにキャストする ~多次元配列編~ | Suinasia
private function obj2arr($obj) { if ( !is_object($obj) ) return $obj; $arr = (array) $obj; foreach ( $arr as &$a ) { $a = $this->obj2arr($a); } return $arr; }
ここでだいたいarrayにキャストできるんだが、元々配列が含まれると、配列以下を見てくれない事に気づいた。
["icon"]=> array(1) { ["label"]=> string(35) "http://itunes.apple.com/favicon.ico" } ["link"]=> array(2) { [0]=> object(stdClass)#59 (1) { ["attributes"]=> ...
"icon" はうまくarrayにキャストできているけど、"link"は元々arrayなので、[0]個目の要素以下がキャストされてない。
しょうがないので以下のようにした。
private function obj2arr($obj) { if ( !is_object($obj) && $this->is_hash($obj) ) return $obj; $arr = (array) $obj; foreach ( $arr as &$a ) { $a = $this->obj2arr($a); } return $arr; } private function is_hash($array) { if (!is_array($array)) { return true; } list($k) = each($array); return $k !== 0; }
closure-stylesheetsでcss compress
cssのcompressしようかな、と。
googleの closure-stylesheets - Closure Stylesheets - Google Project Hosting 使います。
download
Downloads - closure-stylesheets - Closure Stylesheets - Google Project Hosting
ここから最新版落としてきます。
http://closure-stylesheets.googlecode.com/files/closure-stylesheets-20111230.jar
wget http://closure-stylesheets.googlecode.com/files/closure-stylesheets-20111230.jar mv closure-stylesheets-20111230.jar closure-stylesheets.jar
やってみる
javaが入ってれば、一応そのままで使えます。
java -jar closure-stylesheets.jar --help
cssを引数で渡します
java -jar closure-stylesheets.jar style.css
そのままだと、標準出力に出力するんで、output file nameを指定
java -jar closure-stylesheets.jar style.css --output-file compressed_style.css
エラーとか
何個かエラーで弾かれたので、メモとして。
cssの文法間違いがあると、parse errでこけます。
Compiler parsing error: Parse error in style.css at line 553 column 1: } ^
charset書いてると、removeせえ、って言われますね
@charset removed in style.css at line 1 column 1: @charset "UTF-8"; ^
ios専用のpropertyなんかもエラーでます。
tap-highlight-color is an unrecognized property in style.css at line 41 column 1: tap-highlight-color:rgba(65,152,195,0.6); ^
これは回避方法分からなかった。あとで調べる
※追記
分かった。
-
- allowed-unrecognized-properties と --allowed-unrecognized-property が用意されているっぽい。
- allowed-unrecognized-property propery名 ってoption付ければ、エラーなく出力されます。
複数渡したい時は、--allowed-unrecognized-properties を使うのかな?
java -jar closure-stylesheets.jar style.css --allowed-unrecognized-property tap-highlight-color --output-file compressed_style.css
重複。クロスブラウザ対応などで同じ属性を重複して書かないと行けない場合、エラーでちゃいます。
Detected multiple identical, non-alternate declarations in the same ruleset. If this is intentional please use the /* @alternate */ annotation. background-image:[-moz-linear-gradient(top,#94d6ec,#7bc7e0)] in style.css at line 68 column 1: } ^
メッセージにあるように、こういうエラーを抑制する、@alternate という記述があるので、それを利用しましょう。
debianでzncいれてみる
なんか現在のところ、pkgにそれっぽいもの見つけられなかったし、なんかあっても古いversionしかないって情報をどっかで見たので、本家から落としてきていれることに。
あらかじめ必要っぽいものを入れておく
sudo apt-get install build-essential sudo apt-get install libssl-dev sudo apt-get install libperl-dev sudo apt-get install pkg-config sudo apt-get install libc-ares-dev
最新版おとしてくる
wget http://znc.in/releases/znc-0.206.tar.gz tar zxvf znc-0.206.tar.gz ./configure –enable-extra –-enable-perl –-disable-ipv6
なんかエラーでた
... ... checking for swig... no checking for swig2.0... no configure: WARNING: SWIG is not installed. You should look at http://www.swig.org configure: error: swig was not found. Try --disable-perl --disable-python, or download the necessary sources and use --disable-swig. Please check http://en.znc.in/wiki/Modperl or http://en.znc.in/wiki/Modpython for details.
メッセージにあるとおり、
Modperl - ZNC
ここを開くと、
First, you need to use ./configure with option --enable-perl.
To build modperl, you need some files produced by SWIG. They are not shipped with ZNC because of huge size (several megabytes). There're 2 ways to get them:
Install swig. Needed files will be generated automatically when you run make in znc source dir to compile znc.
Download and unpack these files to/modules/modperl/ (ZNC.pm and modperl.i should be in the same dir), and add option --disable-swig to ./configure
なんてことを言われるので、言われた通りにする。
http://people.znc.in/~darthgandalf/znc/modperl/modperl-znc-0.204.tar.bz2 tar jxf modperl-znc-0.204.tar.bz2
解答してできたものを、zncの解答したdir/modules/modperl/ に移動すればok
configureやりなおし
./configure --enable-extra --disable-ipv6 --disable-swig --enable-perl make make install
続いて、confファイルを作る
znc --makeconf と実行すると、対話的にconfファイルを作れて便利
nickとか、IRC serverとか入力するとして、module系はあとでいれればいいので、今は適当に、default多めにエンター
[ ?? ] What port would you like ZNC to listen on? (1025 to 65535): 6668 //ポート [ ?? ] Username (AlphaNumeric): ryoff [ ?? ] IRC server (host only): irc.hogehoge [ ?? ] Launch ZNC now? (yes/no) [yes]: no
最後に、このまま起動するか? って聞かれるので、とりあえず、noにしておく
confができているので確認する
vim ~/.znc/configs/znc.conf 1 // WARNING 2 // 3 // Do NOT edit this file while ZNC is running! 4 // Use webadmin or *admin instead. 5 // 6 // Buf if you feel risky, you might want to read help on /znc saveconfig and /znc rehash. 7 // Also check http://en.znc.in/wiki/Configuration 8 9 Listener4 = 6668 10 11 <User ryoff> 12 Pass = *** 13 Nick = ryoff 14 AltNick = ryoff 15 Ident = ryoff 16 RealName = Got ZNC? 17 Buffer = 100 18 KeepBuffer = false 19 ChanModes = +n 20 21 Server = irc.hogehoge 22 23 <Chan #hogefuga> 24 </Chan> 25 </User>
自分用メモ さくらのレンサバでgit
参考
http://d.hatena.ne.jp/keisukefukuda/20080520/p1
http://blog.earthyworld.com/archives/sakura-install-git/
http://d.hatena.ne.jp/hateka/20110924/1316850271
codeの場所が変わってた。
http://kernel.org/pub/software/scm/git/ 以下に最新版がないので、
http://git-core.googlecode.com/files/ こっちから持ってくる
作業
mkdir ~/local mkdir ~/tmp cd ~/tmp wget http://git-core.googlecode.com/files/git-1.7.6.3.tar.gz tar xvf git-1.7.6.3.tar.gz cd git-1.7.6.3 ./configure --prefix=$HOME/local gmake # gmakeでないと駄目 gmake install
$HOME/local/bin にPATHとおす
自分用メモ さくらレンサバでgitを使う
参考
http://d.hatena.ne.jp/keisukefukuda/20080520/p1
http://blog.earthyworld.com/archives/sakura-install-git/
http://d.hatena.ne.jp/hateka/20110924/1316850271
codeの場所が変わってた。
http://kernel.org/pub/software/scm/git/ 以下に最新版がないので、
http://git-core.googlecode.com/files/ こっちから持ってくる
作業
mkdir ~/local mkdir ~/tmp cd ~/tmp wget http://git-core.googlecode.com/files/git-1.7.6.3.tar.gz tar xvf git-1.7.6.3.tar.gz cd git-1.7.6.3 ./configure --prefix=$HOME/local gmake # gmakeでないと駄目 gmake install
$HOME/local/bin にPATHとおす
bashで日付の妥当性をチェックする
ずいぶん久々の更新です。
はてなブログってなんですか?
うそです。YAPCで発表聞いたので知ってます。
はてなBって書いたら、ブックマークなのかブログなのかわからんす。。。
日付妥当性
雑談はおいておいて、本題です。
yyyymmdd.log みたいなファイルを吐き出してて、そのファイル名をほげほげしようと思ったので、間違ったファイル処理してないかチェックしたいな、と思った。
1 #!/bin/bash 2 3 DATES="20110228 20110229" 4 for DATE in $DATES; do 5 if [ "`date +'%Y%m%d' -d $DATE 2> /dev/null`" == $DATE ]; then 6 echo "$DATE is ok" 7 else 8 echo "$DATE is ng" 9 fi 10 done
date に日付食わせて、同じフォーマットで表示して、結果==だったら、ok。
存在しない日付は、
date: invalid date
とか出るんで、errは捨て。
ダブルクォートで囲わないと、異なる型を比較してるってことで
./date.sh: line 5: [: ==: unary operator expected
こんな感じで怒られるんで、囲ってます。
結果
[ryoff@ryoff00 ~]$ ./date.sh 20110228 is ok 20110229 is ng