0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

CRLFからLFへ

Last updated at Posted at 2020-10-28

備忘録シリーズです...

過去にチームでWindowsのひとがいた際に、大変になったので共有します。
効率的なものがありましたら、教えてください!!

CRLFの改行コード検索

find . -type f | xargs file | grep CRLF

CRLFから変更

☆一つづつ削除

nkf -Lu --overwrite ファイル名orファイルパス

☆一括削除

find . -type f | xargs nkf -Lu --overwrite

☆Gitを含む場合

Gitが壊れて焦ったので、Gitでなくともバイナリファイルは含まないようにした方が良いと思います。

find . -type f | grep -v '.git/' | xargs -n 10 file | grep CRLF | xargs nkf -Lu --overwrite

[処理の流れ]
全ファイルを検索
→Gitディレクトリを含めず検索
→10行づつファイルを出力する
→CRLFを含むファイルを検索
→NKFコマンドで「-Lu(LF)」に変換する

☆Gitを含まない場合

find . -type f | xargs -n 10 file | grep CRLF | xargs nkf -Lu --overwrite

[処理の流れ]
全ファイルを検索
→10行づつファイルを出力する
→CRLFを含むファイルを検索
→NKFコマンドで「-Lu(LF)」に変換する

感想

シェルにして置くと楽そうですね!

201110 追記
shellscript/commandファイルとしてコード化しました!
冗長なのでいずれ直したいと思います。

ソース

changeLF.command
# !/bin/sh

# command実行場所指定
cd `dirname $0`

function command_check {
  command -v "$1" > /dev/null
}

echo "nkfコマンドの存在をチェックします。"
if ! command_check nkf ; then
  if ! command_check brew ; then
    set -e
    echo "Homebrewのインストールをします。"
    echo "Macにログインした際のパスワードを入力してください!"
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
    echo "Homebrewのインストールを完了しました。"
  fi
  brew install nkf
fi
echo "nkfコマンドの存在をチェックしました。"

echo "CRLFファイルを検索します。"
crlf=$(find . -type f | grep -v '.git/' | xargs -n 10 file | grep CRLF)
if [ -f ${crlf} ]; then
  echo "CRLFファイルはありませんでした。"
else
  echo "${crlf}"
  echo "CRLFファイルをLFに変換します。"
  find . -type f | grep -v '.git/' | xargs -n 10 file | grep CRLF | xargs nkf -Lu --overwrite
  echo "CRLFファイルをLFに変換しました。"
fi

echo '処理を終了します。'

参考文献

https://infltech.com/articles/NWnUKJ
https://qiita.com/ionis_h/items/9f9642e435e30a33a88c
https://eng-entrance.com/linux-command-find

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?