Linux Tips

2つのファイルを行ごとに比較にするには

北浦訓行
2004/3/11

 2つのファイルを行ごとに比較にするなら、commコマンドを使う。commコマンドは、ソート済みの2つのファイルを行ごとに比較して、それぞれのファイルにのみ含まれている行と両方のファイルに含まれている行を表示する。例えば、電子メールアドレスを記述した2つのファイルがある。

[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
mail1.txt

[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
mail2.txt

 これをcommコマンドに渡すと、以下のような結果が表示される。

$ comm mail1.txt mail2.txt
                [email protected]
                [email protected]
                [email protected]
                [email protected]
                [email protected]
        [email protected]
        [email protected]
                [email protected]
[email protected]
                [email protected]
[email protected]

 左側の列には、1つ目のファイル(mail1.txt)にのみ含まれている行が表示される。真ん中の列には、2つ目のファイル(mail2.txt)にのみ含まれている行が表示される。右側の列には、両方のファイルに含まれている行が表示される。

 なお、commコマンドでファイルを比較する際は、必ず事前にsortコマンドでファイルを昇順に並べ替えておく必要がある。

Linux Tips Index



 Linux Squareフォーラム Linux Tipsカテゴリ別インデックス
インストール/RPM ブート/ブートローダ
ファイル操作 環境設定
ユーザー管理 コンソール/ターミナル
X Window System セキュリティ
トラブルシューティング 他OS関係
ネットワーク ハードウェア
Webサーバ Samba
GNOME KDE
OpenOffice.org エミュレータ
ソフトウェア そのほか/FAQ
全Tips公開順インデックス Linux Tips月間ランキング
Linux Squareフォーラム全記事インデックス

MONOist組み込み開発フォーラムの中から、Linux関連記事を紹介します


Linux & OSS フォーラム 新着記事
@ITメールマガジン 新着情報やスタッフのコラムがメールで届きます(無料)

注目のテーマ

Linux & OSS 記事ランキング

本日 月間