1
1

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.

Linux基本コマンド sort diff uniq

Last updated at Posted at 2016-08-07

sort

オプション

オプション 説明
-n 数字としてソートする
-t 区切り文字を指定
-k 何列目
-r 逆順

testファイルで確認

```
kuroda male tokyo 70
amy female tokyo 60
yoko male tochigi 80
hana female osaka 75
```

1. ソート

$ sort test 

amy female tokyo 60
hana female osaka 75
kuroda male tokyo 70
yoko male tochigi 80

2. 逆順でソート

$ sort -r test

yoko male tochigi 80
kuroda male tokyo 70
hana female osaka 75
amy female tokyo 60

3. 2列目でソート

$ sort -k 2 test

hana female osaka 75
amy female tokyo 60
yoko male tochigi 80
kuroda male tokyo 70

4. 数字でソート

$ sort -k 4 -n test

amy female tokyo 60
kuroda male tokyo 70
hana female osaka 75
yoko male tochigi 80

利用例

# ファイルサイズでsort
ls -la | sort -rk 5

uniq

ファイル内容をユニックにする、sortと合わせて利用が多い

オプション

オプション 説明
-c 重複数を集計
-i 大文字、小文字区別しない
$ cat uniq.txt
abc
123
abc
123

$ sort uniq.txt | uniq
123
abc

$ sort uniq.txt | uniq -c
2 123
2 abc

diff

```
$ diff -u file1 file2 # 見やすい形式
```

色付きで見る

```
$ brew install colordiff
$ colordiff -u file1 file2
```

関連

linuxのテキスト編集(vi, sed, tr, grepなど+xargs)まとめ

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?