2014-11-04

環境によってsortコマンドの結果が異なる件

CentOS 5からCentOS 6にリモート接続して、sortコマンドで並べ替えを行ってみたところ

CentOS 6上でsortした結果とリモート接続してsortした結果が異なってしまい嵌ったので解決策をφ(・ω・ )メモメモ

元ファイル
  1. [centos6] $ cat /tmp/test  
  2. a  
  3. "  
  4. z  
  5. /  
  6. _  

CentOS 6上でのsort結果
  1. [centos6] $ sort /tmp/test  
  2. _  
  3. /  
  4. "  
  5. a  
  6. z  

CentOS 5 -> CentOS 6へリモートコマンド実行kした際のsort結果
  1. [centos5] $ ssh centos6 "sort /tmp/test"  
  2. "  
  3. /  
  4. _  
  5. a  
  6. z  

と、いう感じで記号の並び順が異なってしまっている。

リモートコマンドで実行していることが原因なのかと思い、同一のファイルをCentOS 5上にも作成してCentOS5上でのsort結果も試してみた
  1. [centos5] $ sort /tmp/test  
  2. "  
  3. /  
  4. _  
  5. a  
  6. z  

どうやら、リモート実行だからという訳では無いようで実行元の環境に依存している雰囲気でした。

いろいろと試行錯誤してたどり着いた解決策はこれ→LC_ALL=Cをsortコマンドの前に指定してあげる

CentOS 6上でのsort結果
  1. [centos6] $ LC_ALL=C sort /tmp/test  
  2. "  
  3. /  
  4. _  
  5. a  
  6. z  

CentOS 5 -> CentOS 6へリモートコマンド実行kした際のsort結果
  1. [centos5] $ ssh centos6 "LC_ALL=C sort /tmp/test"  
  2. "  
  3. /  
  4. _  
  5. a  
  6. z  

これで無事sort結果がそろいましたヾ(*・∀・)ノ"

0 件のコメント:

コメントを投稿