このページの内容

7.20 すべての重複行の削除

このスクリプトは、「uniq -u」と同様に、重複しない行だけを出力します。

#!/usr/bin/sed -f

# Search for a duplicate line --- until that, print what you find.
$b
N
/^\(.*\)\n\1$/ ! {
    P
    D
}
コード内の説明(日本語訳)

重複行を探し、それまでは見つけた行を出力します。



:c
# Got two equal lines in pattern space.  At the
# end of the file we simply exit
$d
コード内の説明(日本語訳)

パターンスペースに同じ行が2行入りました。ファイルの終わりなら、そのまま終了します。



# Else, we keep reading lines with N until we
# find a different one
s/.*\n//
N
/^\(.*\)\n\1$/ {
    bc
}
コード内の説明(日本語訳)

そうでなければ、異なる行が見つかるまでNで行を読み続けます。



# Remove the last instance of the duplicate line
# and go back to the top
D
コード内の説明(日本語訳)

重複行の最後の1行を取り除き、先頭へ戻ります。