LoginSignup
0
0

More than 1 year has passed since last update.

uniq -c のパディングやめて、区切り文字を変更する

Posted at

(備忘録な記事)
Linuxコマンドで、テキストファイルの各行を一意(ユニーク)にして、被りが何行だったかカウントを表示する際のお話

cat a.tsv b.tsv | sort | uniq -c > result.txt

上記で処理すると、各行の先頭に、被りの行数が左パディング(半角スペースで桁揃え)されて、右に半角スペース1つで区切って、実際の各行の内容が出力されます。

   1 a-text    01
 109 a-text    02
  12 a-text    03
   1 a-text    04
   1 b-text    01
1234 b-text    02
  45 b-text    03

(表示の都合上、タブを4スペースで表現してます)

これをパディングなしのタブ区切り(TSV)にします。
(エクセルに読み込むとか、元のファイルがtsvとか)

cat a.tsv b.tsv | sort | uniq -c | awk -F" " '{print $1"\t"$2"\t"$3}' > result.tsv

awkの使い方がダサいですが、手っ取り早く、これで。

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