27.9. sort - sort lines of text files

简介:
$ du -s * | sort -k1,1rn
	
$ rpm -q -a --qf '%10{SIZE}\t%{NAME}\n' | sort -k1,1n
$ dpkg-query -W -f='${Installed-Size;10}\t${Package}\n' | sort -k1,1n
	

27.9.1. 对列排序

sort -k 具体说来, 你可以使用 -k1,1 来对第一列排序, -k1来对全行排序

# sort -t ':' -k 1 /etc/passwd
		
ort -n -t ‘ ‘ -k 2 file.txt
		

多列排序

$ sort -n -t ‘ ‘ -k 2 -k 3 file.txt
		

27.9.2. -s, --stable stabilize sort by disabling last-resort comparison

例如: 如果你要想对两例排序, 先是以第二列, 然后再以第一列, 那么你可以这样. sort -s 会很有用

 sort -k1,1 | sort -s -k2,2		
		

Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a>




原文出处:Netkiller 系列 手札
本文作者:陈景峯
转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。

目录
相关文章
|
10月前
|
Java
Leetcode 295. Find Median from Data Stream
在一个有序数组中找中位数,但需要支持再数组中添加新的元素。本来是有序里的,可以很轻易就查到中位数,但如果添加新数字后,不一定有序。如果先对数组排序,那代价就比较大了,每次排序时间复杂度O(n*log(n)),看discuss发现了一种很巧妙的解法,可以把添加数据的时间复杂度降低到O(log(n)) ,查询中位数O(1)。
45 0
|
算法
LeetCode Find Minimum in Rotated Sorted Array II
假设按照升序排序的数组在预先未知的某个点上进行了旋转。 ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。 请找出其中最小的元素。 注意数组中可能存在重复的元素。
80 0
LeetCode Find Minimum in Rotated Sorted Array II
LeetCode 153. Find Minimum in Rotated Sorted Array
假设按照升序排序的数组在预先未知的某个点上进行了旋转。 ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。 请找出其中最小的元素。 你可以假设数组中不存在重复元素。
100 0
LeetCode 153. Find Minimum in Rotated Sorted Array
|
算法 Python
LeetCode 295. Find Median from Data Stream
中位数是有序列表中间的数。如果列表长度是偶数,中位数则是中间两个数的平均值。
90 0
LeetCode 295. Find Median from Data Stream
|
C++
[LeetCode] Find Median from Data Stream
This post shares a very nice solution using two heaps: a max heap for the smaller half and a min heap for the larger half.
757 0
|
Perl
[LeetCode] Find Minimum in Rotated Sorted Array II
This problem is more or less the same as Find Minimum in Rotated Sorted Array. And one key difference is as stated in the solution tag.
782 0
|
C++ Python
[LeetCode] Find Minimum in Rotated Sorted Array
As explained in the Solution tag, the key to solving this problem is to use invariants. We set two pointers: l for the left and r for the right.
726 0