我有以下形式的数据:
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:14
Size:2
Size:1
Size:1
Size:1
Size:1
Size:3
Size:3
Size:3
我想找出与大小相关的所有值的出现次数。例如,14次发生一次,2次发生一次,1次发生四次,等等,按排序顺序((i).sorted按发生次数排列,(ii).sorted按与大小相关的值排列)。这是希望以排序的方式获得以下结果
(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。
发布于 2015-02-11 22:50:19
要提取大小字段,
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]*\)/\2->\1/'
https://stackoverflow.com/questions/28470594
复制相似问题