需要根据2个条件进行grep和排序记录

时间:2015-09-09 17:44:09

标签: shell sorting grep

我要求对记录进行grep和排序,首先是mdmId(你可以看到它出现在文本mdmId朝向行尾),然后是行号或时间戳(出现在行的开头)

当前输出:

$ grep -F -n -f grp_param cmxserver.log
43723:[2015-09-09 11:18:14,819] [threadExecutor-8] [INFO ] com.delos: CustomerSaveHandler - beforeEverything -  mdmId is : 834699
43896:[2015-09-09 11:18:16,085] [threadExecutor-8] [INFO ] com.delos: Completed the beforeEverything -  mdmId is :
43900:[2015-09-09 11:18:16,086] [threadExecutor-8] [INFO ] com.delos: *****CustomerSaveHandler - beforeSave  **** -  mdmId is :
43902:[2015-09-09 11:18:16,086] [threadExecutor-8] [INFO ] com.delos: CustomerSaveHandler - beforeSave - beforeSave - CustomerRelationship - The mdmId is : 834699
45057:[2015-09-09 11:18:52,952] [threadExecutor-10] [INFO ] com.delos: CustomerSaveHandler - beforeEverything -  mdmId is : 834699
45247:[2015-09-09 11:18:54,067] [threadExecutor-1] [INFO ] com.delos: CustomerSaveHandler - beforeEverything -  mdmId is : 834947
45296:[2015-09-09 11:18:54,093] [threadExecutor-10] [INFO ] com.delos: Completed the beforeEverything -  mdmId is :
45300:[2015-09-09 11:18:54,100] [threadExecutor-10] [INFO ] com.delos: *****CustomerSaveHandler - beforeSave  **** -  mdmId is :
45302:[2015-09-09 11:18:54,100] [threadExecutor-10] [INFO ] com.delos: CustomerSaveHandler - beforeSave - beforeSave - CustomerRelationship - The mdmId is : 834699
45623:[2015-09-09 11:18:55,603] [threadExecutor-1] [INFO ] com.delos: Completed the beforeEverything -  mdmId is :
45627:[2015-09-09 11:18:55,605] [threadExecutor-1] [INFO ] com.delos: *****CustomerSaveHandler - beforeSave  **** -  mdmId is :
45629:[2015-09-09 11:18:55,605] [threadExecutor-1] [INFO ] com.delos: CustomerSaveHandler - beforeSave - beforeSave - CustomerRelationship - The mdmId is : 834947
45779:[2015-09-09 11:18:57,161] [threadExecutor-1] [INFO ] com.delos: CustomerSaveHandler - beforeSave - beforeSave - CustomerRelationship - The mdmId is : 834947

期望的输出:

43896:[2015-09-09 11:18:16,085] [threadExecutor-8] [INFO ] com.delos: Completed the beforeEverything -  mdmId is :
43900:[2015-09-09 11:18:16,086] [threadExecutor-8] [INFO ] com.delos: *****CustomerSaveHandler - beforeSave  **** -  mdmId is :
45296:[2015-09-09 11:18:54,093] [threadExecutor-10] [INFO ] com.delos: Completed the beforeEverything -  mdmId is :
45300:[2015-09-09 11:18:54,100] [threadExecutor-10] [INFO ] com.delos: *****CustomerSaveHandler - beforeSave  **** -  mdmId is :
45623:[2015-09-09 11:18:55,603] [threadExecutor-1] [INFO ] com.delos: Completed the beforeEverything -  mdmId is :
45627:[2015-09-09 11:18:55,605] [threadExecutor-1] [INFO ] com.delos: *****CustomerSaveHandler - beforeSave  **** -  mdmId is :
43723:[2015-09-09 11:18:14,819] [threadExecutor-8] [INFO ] com.delos: CustomerSaveHandler - beforeEverything -  mdmId is : 834699
43902:[2015-09-09 11:18:16,086] [threadExecutor-8] [INFO ] com.delos: CustomerSaveHandler - beforeSave - beforeSave - CustomerRelationship - The mdmId is : 834699
45057:[2015-09-09 11:18:52,952] [threadExecutor-10] [INFO ] com.delos: CustomerSaveHandler - beforeEverything -  mdmId is : 834699
45302:[2015-09-09 11:18:54,100] [threadExecutor-10] [INFO ] com.delos: CustomerSaveHandler - beforeSave - beforeSave - CustomerRelationship - The mdmId is : 834699
45247:[2015-09-09 11:18:54,067] [threadExecutor-1] [INFO ] com.delos: CustomerSaveHandler - beforeEverything -  mdmId is : 834947
45629:[2015-09-09 11:18:55,605] [threadExecutor-1] [INFO ] com.delos: CustomerSaveHandler - beforeSave - beforeSave - CustomerRelationship - The mdmId is : 834947
45779:[2015-09-09 11:18:57,161] [threadExecutor-1] [INFO ] com.delos: CustomerSaveHandler - beforeSave - beforeSave - CustomerRelationship - The mdmId is : 834947

您可以在Desired输出中看到,第一个带有mdmId null的记录被选中并按行号(或时间戳)排序。第二个是Id 834699,最后是Id 834947。

我们非常感谢您的帮助。

由于

1 个答案:

答案 0 :(得分:2)

将此附加到您的grep:

| awk '{$1=$NF" "$1; print}' | sort -k1,2n | cut -d " " -f 2-

输出:

43896:[2015-09-09 11:18:16,085] [threadExecutor-8] [INFO ] com.delos: Completed the beforeEverything - mdmId is :
43900:[2015-09-09 11:18:16,086] [threadExecutor-8] [INFO ] com.delos: *****CustomerSaveHandler - beforeSave **** - mdmId is :
45296:[2015-09-09 11:18:54,093] [threadExecutor-10] [INFO ] com.delos: Completed the beforeEverything - mdmId is :
45300:[2015-09-09 11:18:54,100] [threadExecutor-10] [INFO ] com.delos: *****CustomerSaveHandler - beforeSave **** - mdmId is :
45623:[2015-09-09 11:18:55,603] [threadExecutor-1] [INFO ] com.delos: Completed the beforeEverything - mdmId is :
45627:[2015-09-09 11:18:55,605] [threadExecutor-1] [INFO ] com.delos: *****CustomerSaveHandler - beforeSave **** - mdmId is :
43723:[2015-09-09 11:18:14,819] [threadExecutor-8] [INFO ] com.delos: CustomerSaveHandler - beforeEverything - mdmId is : 834699
43902:[2015-09-09 11:18:16,086] [threadExecutor-8] [INFO ] com.delos: CustomerSaveHandler - beforeSave - beforeSave - CustomerRelationship - The mdmId is : 834699
45057:[2015-09-09 11:18:52,952] [threadExecutor-10] [INFO ] com.delos: CustomerSaveHandler - beforeEverything - mdmId is : 834699
45302:[2015-09-09 11:18:54,100] [threadExecutor-10] [INFO ] com.delos: CustomerSaveHandler - beforeSave - beforeSave - CustomerRelationship - The mdmId is : 834699
45247:[2015-09-09 11:18:54,067] [threadExecutor-1] [INFO ] com.delos: CustomerSaveHandler - beforeEverything - mdmId is : 834947
45629:[2015-09-09 11:18:55,605] [threadExecutor-1] [INFO ] com.delos: CustomerSaveHandler - beforeSave - beforeSave - CustomerRelationship - The mdmId is : 834947
45779:[2015-09-09 11:18:57,161] [threadExecutor-1] [INFO ] com.delos: CustomerSaveHandler - beforeSave - beforeSave - CustomerRelationship - The mdmId is : 834947

请参阅:RegEx Demo