LoginSignup
0
0

More than 5 years have passed since last update.

UTF-8 へ一括変換&「¥」を「\」へ書き換えるshスクリプト

Posted at

ファイルのエンコーヂングを一括変換する需要があったので、車輪の再発明かもしれませんが自分の勉強がてら書いてみました。
カレントディレクトリ以下にある全てのencoding Shift-JISのファイルをUTF-8に変換し、全ての¥\に書き換えます。

encoding_converter.sh
#!/bin/sh
dir="`pwd`"
# $dir以下の全てのファイルに対して行う。
for file in `find $dir -type f`;
do
  echo $file
  # encoding_converter.sh自身だった場合にスキップする。
  if expr $file : ".*$0" > /dev/null; then
    echo "$0 itself. skip!"
    continue
  fi

  # すでにutf-8になっていた場合はtmpfに移すだけ。
  if expr "`file -bI $file`" : ".*utf-8" > /dev/null; then
    echo 'already utf-8'
    mv $file tmpf
  # そうでなければshift-jisからutf-8に変換する。
  else
    echo 'change encoding'
    iconv -f shift-jis -t utf-8 $file > tmpf
  fi
  # 変換されたものの¥を\に書き換える。
  echo 'replace ¥ with \\'
  tr '¥' '\\' < tmpf > $file
done
rm tmpf
exit

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0