使用 linux 命令排序
Sorting using linux commands
我有以下形式的数据:
Sub: Size:14Val: 4644613 Some long string here
Sub: Size:2Val: 19888493 Some other long string here
Sub: Size:1Val: 6490281 Some other long string here1
Sub: Size:1Val: 320829337 Some other long string here2
Sub: Size:1Val: 50281086 Some other long string here3
Sub: Size:1Val: 209077847 Some other long string here4
Sub: Size:3Val: 320829337 Some other long string here2
Sub: Size:3Val: 50281086 Some other long string here3
Sub: Size:3Val: 209077847 Some other long string here4
现在我想从该文件中提取所有 Size:-- 信息。那就是我要提取以下内容:
Size:14
Size:2
Size:1
Size:1
Size:1
Size:1
Size:3
Size:3
Size:3
我想找出与大小相关的所有值的出现次数。例如。 14 出现一次,2 出现一次,1 出现四次,依此类推((i).按出现次数排序和 (ii).按与大小关联的值排序))。那就是要以排序的方式得到以下结果
(i). sorted by number of occurences
1->4
3->3
2->1
14->1
(ii). sorted by the value associated with Size:
1->4
2->1
3->3
14->1
我写了一个 python 程序并且能够对它们进行排序。但我在想是否有某种方法可以使用 grep 等 linux 命令来做同样的事情?我正在使用 ubuntu 12.04.
要提取尺寸字段,
grep -o 'Size:[0-9]*' data
可以使用 sort | uniq -c | sort -rn
按唯一出现次数排序,您可以对第一个 sort
进行一些小的修改(即添加 -t : -k2rn
)并省略 sort -rn
最后按值排序。使用简单的 sed
脚本可以轻松地将最终输出转换为您需要的格式。
grep -o 'Size:[0-9]*' data |
sort -t : -k2rn | uniq -c |
sed 's/^ *//;s/\([1-9][0-9]*\) Size:\([0-9]*\)/->/'
我有以下形式的数据:
Sub: Size:14Val: 4644613 Some long string here
Sub: Size:2Val: 19888493 Some other long string here
Sub: Size:1Val: 6490281 Some other long string here1
Sub: Size:1Val: 320829337 Some other long string here2
Sub: Size:1Val: 50281086 Some other long string here3
Sub: Size:1Val: 209077847 Some other long string here4
Sub: Size:3Val: 320829337 Some other long string here2
Sub: Size:3Val: 50281086 Some other long string here3
Sub: Size:3Val: 209077847 Some other long string here4
现在我想从该文件中提取所有 Size:-- 信息。那就是我要提取以下内容:
Size:14
Size:2
Size:1
Size:1
Size:1
Size:1
Size:3
Size:3
Size:3
我想找出与大小相关的所有值的出现次数。例如。 14 出现一次,2 出现一次,1 出现四次,依此类推((i).按出现次数排序和 (ii).按与大小关联的值排序))。那就是要以排序的方式得到以下结果
(i). sorted by number of occurences
1->4
3->3
2->1
14->1
(ii). sorted by the value associated with Size:
1->4
2->1
3->3
14->1
我写了一个 python 程序并且能够对它们进行排序。但我在想是否有某种方法可以使用 grep 等 linux 命令来做同样的事情?我正在使用 ubuntu 12.04.
要提取尺寸字段,
grep -o 'Size:[0-9]*' data
可以使用 sort | uniq -c | sort -rn
按唯一出现次数排序,您可以对第一个 sort
进行一些小的修改(即添加 -t : -k2rn
)并省略 sort -rn
最后按值排序。使用简单的 sed
脚本可以轻松地将最终输出转换为您需要的格式。
grep -o 'Size:[0-9]*' data |
sort -t : -k2rn | uniq -c |
sed 's/^ *//;s/\([1-9][0-9]*\) Size:\([0-9]*\)/->/'