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 5 years have passed since last update.

diffでテキストファイルの違う行だけ表示したい

Posted at

diff

diff data1.txt data2.txt

と普通に使うと

--- data1.txt
+++ data2.txt
@@ -1,4 +1,4 @@
 11111
-22222
+22225
 33333
-44444
+44445

のように内容が同じ行と違う行とが混じって表示されます。
行数が多い時にはちょっと見づらいなと思いました。

sed

同じ行の先頭は空白なので、sedでフィルタ(削除)します。

sed -e '/^ .\+/d'

「^ 」先頭が空白で始まる「.\+」任意の文字列をdで削除という意味です。

diffとパイプでつなぎます。

diff data1.txt data2.txt | sed -e '/^ .\+/d'

すると

--- data1.txt
+++ data2.txt
@@ -1,4 +1,4 @@
-22222
+22225
-44444
+44445

違う行だけになってスッキリしました。

最近Windows上でbusyboxを使い、シェルの勉強をしているんですが奥が深いです。

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?