我有一个Bash脚本,它在日志目录上执行一些awk魔法,并将结果合并到一个文件中...问题是日志包含第1行的日期和第2行的数据,第3行的日期和第4行的数据等等。所以当我对它进行排序时,所有日期都排在最前面,所有数据都在底部排序......顺便说一下,日期不按顺序排列,所以我想对它们进行排序,这样它们就可以了是...
有没有办法对每两行进行分组,然后根据该组的第一行进行排序......
日期和数据分为两行
EXECUTING出现在每个日期行的末尾,所以我确信这可能是关键但不确定使用什么工具来实现它。
以下是预排序输出的示例
2014-07-27 12:01:01,893] [xxxxxxxxxxx] [ERROR] [xxxxxxxxxx] [xxxxxxxxxxx] EXECUTING
[5205003] Unable to find a user for user name or alias xxxxxx in group default. (Ref:161086252)
2014-07-27 12:00:01,428] [xxxxxxxxxxx] [ERROR] [xxxxxxxxxx] [xxxxxxxxxxx] EXECUTING
[5205003] Unable to find a user for user name or alias xxxxxx in group default. (Ref:1961536860)
2014-07-27 12:01:01,428] [xxxxxxxxxxx] [ERROR] [xxxxxxxxxx] [xxxxxxxxxxx] EXECUTING
[5205003] Unable to find a user for user name or alias xxxxxx in group default. (Ref:1961536860)
2014-07-27 12:00:01,428] [xxxxxxxxxxx] [ERROR] [xxxxxxxxxx] [xxxxxxxxxxx] EXECUTING
[5205003] Unable to find a user for user name or alias xxxxxx in group default. (Ref:1961536860)
2014-07-27 12:00:01,428] [xxxxxxxxxxx] [ERROR] [xxxxxxxxxx] [xxxxxxxxxxx] EXECUTING
[5205003] Unable to find a user for user name or alias xxxxxx in group default. (Ref:1961536860)
以下是排序后的样本(sort some_file.log)
2014-07-27 12:00:01,428] [xxxxxxxxxxx] [ERROR] [xxxxxxxxxx] [xxxxxxxxxxx] EXECUTING
2014-07-27 12:00:01,428] [xxxxxxxxxxx] [ERROR] [xxxxxxxxxx] [xxxxxxxxxxx] EXECUTING
2014-07-27 12:00:01,428] [xxxxxxxxxxx] [ERROR] [xxxxxxxxxx] [xxxxxxxxxxx] EXECUTING
2014-07-27 12:01:01,428] [xxxxxxxxxxx] [ERROR] [xxxxxxxxxx] [xxxxxxxxxxx] EXECUTING
2014-07-27 12:01:01,893] [xxxxxxxxxxx] [ERROR] [xxxxxxxxxx] [xxxxxxxxxxx] EXECUTING
[5205003] Unable to find a user for user name or alias xxxxxx in group default. (Ref:161086252)
[5205003] Unable to find a user for user name or alias xxxxxx in group default. (Ref:1961536860)
[5205003] Unable to find a user for user name or alias xxxxxx in group default. (Ref:1961536860)
[5205003] Unable to find a user for user name or alias xxxxxx in group default. (Ref:1961536860)
[5205003] Unable to find a user for user name or alias xxxxxx in group default. (Ref:1961536860)
以下是所需输出的示例
2014-07-27 12:00:01,428] [xxxxxxxxxxx] [ERROR] [xxxxxxxxxx] [xxxxxxxxxxx] EXECUTING
[5205003] Unable to find a user for user name or alias xxxxxx in group default. (Ref:1961536860)
2014-07-27 12:00:01,428] [xxxxxxxxxxx] [ERROR] [xxxxxxxxxx] [xxxxxxxxxxx] EXECUTING
[5205003] Unable to find a user for user name or alias xxxxxx in group default. (Ref:1961536860)
2014-07-27 12:00:01,428] [xxxxxxxxxxx] [ERROR] [xxxxxxxxxx] [xxxxxxxxxxx] EXECUTING
[5205003] Unable to find a user for user name or alias xxxxxx in group default. (Ref:1961536860)
2014-07-27 12:01:01,428] [xxxxxxxxxxx] [ERROR] [xxxxxxxxxx] [xxxxxxxxxxx] EXECUTING
[5205003] Unable to find a user for user name or alias xxxxxx in group default. (Ref:1961536860)
2014-07-27 12:01:01,893] [xxxxxxxxxxx] [ERROR] [xxxxxxxxxx] [xxxxxxxxxxx] EXECUTING
[5205003] Unable to find a user for user name or alias xxxxxx in group default. (Ref:161086252)
Rici的回答是对我有用的......
cat test.log | paste - - | sort | sed 's/\t/\n/' > testsort
答案 0 :(得分:0)
您可以按照第一列的值进行排序
sort -k1,1nr -k2,2 logfile
答案 1 :(得分:0)
正如@chrisaycock在评论中指出的那样,如果日期和数据在同一条线上,您可以轻松地对这些进行排序。如果你没有一个简单的方法在你的“awk魔法”中做到这一点,你可以事后做到;如果有一些你肯定知道不在任何日期行中的字符会很容易,如果该字符是 TAB ,那就更容易了。
如果您知道任何日期行中没有制表符:
awk magic files | paste - - | sort | sed 's/\t/\n/'
如果日期行中有标签,但没有 | :
awk magic files | paste -d'|' - - | sort | sed 's/|/\n/'
调用paste - -
一次从标准输入读取两行,并以分隔符(默认选项卡,使用-d
标志覆盖)输出它们。您可以使用更多-
来扩展它,并且可以指定一系列分隔符:
$ seq 1 9 | paste -d:+ - - -
1:2+3
4:5+6
7:8+9