我正在尝试对以下文字进行排序:
Tue 2014 Nov 25 07:44:44 berlin VPN connection succeed
Tue 2014 Nov 25 08:01:12 paris VPN connection succeed
Tue 2014 Nov 25 08:44:45 berlin VPN connection succeed
Tue 2014 Nov 25 09:01:18 paris VPN connection succeed
Tue 2014 Nov 25 09:12:52 london VPN connection succeed
Tue 2014 Nov 25 09:44:45 berlin VPN connection succeed
Tue 2014 Nov 25 10:44:50 berlin VPN connection succeed
Tue 2014 Nov 25 10:57:34 dallas VPN connection succeed
Tue 2014 Nov 25 11:35:56 london VPN connection succeed
Tue 2014 Nov 25 11:47:05 amsterdam VPN connection succeed
Tue 2014 Nov 25 11:57:34 dallas VPN connection succeed
Tue 2014 Nov 25 12:47:06 amsterdam VPN connection succeed
Tue 2014 Nov 25 12:57:33 dallas VPN connection succeed
Tue 2014 Nov 25 13:47:06 amsterdam VPN connection succeed
Tue 2014 Nov 25 13:57:33 dallas VPN connection succeed
Tue 2014 Nov 25 14:47:07 amsterdam VPN connection succeed
Tue 2014 Nov 25 14:57:33 dallas VPN connection succeed
Tue 2014 Nov 25 09:14:07 london VPN connection disconnected
Tue 2014 Nov 25 09:35:05 paris VPN connection disconnected
Tue 2014 Nov 25 10:57:26 amsterdam VPN connection disconnected
Tue 2014 Nov 25 11:35:41 berlin VPN connection disconnected
Tue 2014 Nov 25 11:39:00 london VPN connection disconnected
我想按2个标准排序:
1.首先排序第6列(城市名称)
2.然后按时间排序(第5列)
该文本来自一个名为text.txt的文件,我使用cat命令。
我使用以下命令:
cat text.txt | sort -k6 -k5,1n
然而,它产生以下输出(它对城市名称进行排序,但不对时间字段进行排序):
Tue 2014 Nov 25 10:57:26 amsterdam VPN connection disconnected
Tue 2014 Nov 25 11:47:05 amsterdam VPN connection succeed
Tue 2014 Nov 25 12:47:06 amsterdam VPN connection succeed
Tue 2014 Nov 25 13:47:06 amsterdam VPN connection succeed
Tue 2014 Nov 25 14:47:07 amsterdam VPN connection succeed
Tue 2014 Nov 25 11:35:41 berlin VPN connection disconnected
Tue 2014 Nov 25 07:44:44 berlin VPN connection succeed
Tue 2014 Nov 25 08:44:45 berlin VPN connection succeed
Tue 2014 Nov 25 09:44:45 berlin VPN connection succeed
Tue 2014 Nov 25 10:44:50 berlin VPN connection succeed
Tue 2014 Nov 25 10:57:34 dallas VPN connection succeed
Tue 2014 Nov 25 11:57:34 dallas VPN connection succeed
Tue 2014 Nov 25 12:57:33 dallas VPN connection succeed
Tue 2014 Nov 25 13:57:33 dallas VPN connection succeed
Tue 2014 Nov 25 14:57:33 dallas VPN connection succeed
Tue 2014 Nov 25 09:14:07 london VPN connection disconnected
Tue 2014 Nov 25 11:39:00 london VPN connection disconnected
Tue 2014 Nov 25 09:12:52 london VPN connection succeed
Tue 2014 Nov 25 11:35:56 london VPN connection succeed
Tue 2014 Nov 25 09:35:05 paris VPN connection disconnected
Tue 2014 Nov 25 08:01:12 paris VPN connection succeed
Tue 2014 Nov 25 09:01:18 paris VPN connection succeed
如何解决此问题,以便我可以对1.城市名称进行排序,然后在时间字段上进行排序?
有人可以帮我这个吗?
答案 0 :(得分:3)
您可以简化sort
。这个有效:
sort -k6,6 file
Tue 2014 Nov 25 10:57:26 amsterdam VPN connection disconnected
Tue 2014 Nov 25 11:47:05 amsterdam VPN connection succeed
Tue 2014 Nov 25 12:47:06 amsterdam VPN connection succeed
Tue 2014 Nov 25 13:47:06 amsterdam VPN connection succeed
Tue 2014 Nov 25 14:47:07 amsterdam VPN connection succeed
Tue 2014 Nov 25 07:44:44 berlin VPN connection succeed
Tue 2014 Nov 25 08:44:45 berlin VPN connection succeed
Tue 2014 Nov 25 09:44:45 berlin VPN connection succeed
Tue 2014 Nov 25 10:44:50 berlin VPN connection succeed
Tue 2014 Nov 25 11:35:41 berlin VPN connection disconnected
Tue 2014 Nov 25 10:57:34 dallas VPN connection succeed
Tue 2014 Nov 25 11:57:34 dallas VPN connection succeed
Tue 2014 Nov 25 12:57:33 dallas VPN connection succeed
Tue 2014 Nov 25 13:57:33 dallas VPN connection succeed
Tue 2014 Nov 25 14:57:33 dallas VPN connection succeed
Tue 2014 Nov 25 09:12:52 london VPN connection succeed
Tue 2014 Nov 25 09:14:07 london VPN connection disconnected
Tue 2014 Nov 25 11:35:56 london VPN connection succeed
Tue 2014 Nov 25 11:39:00 london VPN connection disconnected
Tue 2014 Nov 25 08:01:12 paris VPN connection succeed
Tue 2014 Nov 25 09:01:18 paris VPN connection succeed
Tue 2014 Nov 25 09:35:05 paris VPN connection disconnected
这是有效的,因为您的时间戳使用HH:MM:SS
格式。
答案 1 :(得分:1)
如果您执行sort -k 6
,则会对字段6,然后是7,8,9等进行排序。从手册中:
KEYDEF是F [.C] [OPTS] [,F [.C] [OPTS]]的开始和停止位置, 其中F是字段编号,C是字段中的字符位置; 两者都是原点1,停止位置默认为该行 端强>
如果您只想对字段6进行排序,请将其明确设置为开始和停止字段:
sort -k 6,6
如果要对6进行排序,然后对5进行排序,请执行以下操作:
sort -k 6,6 -k 5,5