2
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?

【小技】IBMi に dos2unix を入れて手早く改行コードを変換する

Last updated at Posted at 2025-12-16

IFS上にあるテキストファイルの改行コード CR,LF(0x0d,0x0a)LF(0x0a)CR(0x0d) 間を IBM i で手早く変換する方法を紹介します。

dos2unix を導入する

yum 導入済み IBM i 環境である必要があります。以下のコマンドでdos2unix をインストールします。

yum install -y dos2unix
~~~中略~~~
インストール:
  dos2unix.ppc64 0:7.4.1-1

完了しました!

テキストファイルを作成する

適当なテキストファイルを作成します。

# ワークディレクトリへ移動
cd /home/ushiday/temp/qiita/2025/20251217

# ファイル作成
echo "ABC" > abc-lf.txt

改行を変換する

dos2unix を導入すると以下のコマンドが使えます。詳細は man dos2unix で確認して下さい。

  • dos2unix : CR,LFLF へ変換
  • unix2dos : LFCR,LF へ変換
  • unix2mac : LFCR へ変換

以下の様に改行コードを変換できます。-n オプションで 入力ファイル 出力ファイル 形式が指定できます。 -n 指定しない場合は元のファイルが上書きされます。

# abc-lf.txt 改行コードを確認
od -Ax -tx1z abc-lf.txt
000000 41 42 43 0a                                      >ABC.<
000004

# CR+LF に変換する
unix2dos -n abc-lf.txt abc-crlf.txt
unix2dos: converting file abc-lf.txt to file abc-crlf.txt in DOS format...

# abc-crlf.txt 改行コードを確認
od -Ax -tx1z abc-crlf.txt
000000 41 42 43 0d 0a                                   >ABC..<
000005

# CR に変換する
unix2mac -n abc-lf.txt abc-cr.txt
unix2mac: converting file abc-lf.txt to file abc-cr.txt in Mac format...

# abc-cr.txt 改行コードを確認
od -Ax -tx1z abc-cr.txt
000000 41 42 43 0d                                      >ABC.<
000004

おまけ( BOM 付与/除去)

dos2unix を使って、BOM 付き/除去もできます。データやり取りする時に BOM ありだったり、なしだったり結構バラバラなので便利です。

# ファイルをBOM付きにする
dos2unix -m abc-lf.txt
dos2unix: converting file abc-lf.txt to Unix format...

# abc-lf.txt BOM付与を確認
od -Ax -tx1z abc-lf.txt
000000 ef bb bf 41 42 43 0a                             >ABC.<
000007

# ファイルからBOMを除去する
dos2unix -r abc-lf.txt
dos2unix: converting file abc-lf.txt to Unix format...

# abc-lf.txt BOM付与を確認
od -Ax -tx1z abc-lf.txt
000000 41 42 43 0a                                      >ABC.<
000004

他にも便利なオプションがあると思うので、色々掘り下げてみると良いと思います。

2
0
2

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
2
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?