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 1 year has passed since last update.

定時帰る術-Linux改行問題(cat、fileコマンド)

Posted at

改行問題

Windowsで作ったファイルをそのままでlinuxにアップロードする場合、linuxが認識できない改行がついていることがあり、cat -Aで確認できる。

問題:↑でサービス起動で謎のエラーがでたり、シェルの実行でエラーがでたりする。

改行がおかしい例

cat –A test.txt
a^M$
b^M$
c^M$
d^M$
e^M$
f^M$

「^M」が表示されたら、改行がおかしいと判断できる

正常の場合:

cat -A test2.txt
a$
b$
c$
d$
e$
f$

file で確認した方がはやいかも

file test.txt
test.txt: ASCII text, with CRLF line terminators

赤字の部分(with CRLF line terminators)が表示されたら、改行がおかしいと判断できる

解決:

WinSCPでWindows→linuxファイルを転送する時、転送設定を「テキスト」にしてから、転送するようにする。

(↑で日本語が存在する場合、文字化けが起きる場合があります。)

Windowsの改行はCR(\r)+LF(\n)となり、linuxはLF(\n)なのでCRを消せば、OKです。

sed -i 's/\r//g' test.txt

(-iを付けずに、確認してから-iで実行した方が安全)

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?