2015-02-12 83 views
1

我在下面的表格数据:排序使用Linux命令

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 

而我想找出所有与大小相关的值的出现次数。例如。 (i)按发生次数分类,(ii)按照与大小相关的值分类),一次发生一次,2次发生一次,1次发生四次等等))。这就是想要以排序的方式得到如下结果:

(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。

回答

1

要提取大小字段,

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/'