AWK是否可以读取时间字段并将其用于排序? [英] AWK is it possible to read a time field and use it for sorting?

查看:345
本文介绍了AWK是否可以读取时间字段并将其用于排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文件,我需要根据时间列对行进行排序和合并:

I have two files and I need to sort and merge the rows based on the time column:

文件A:

"2014-02-26 16:03:04"   "Login Success|isNoSession=false"   id=csr,ou=user,dc=openam,dc=forgerock,dc=org    7efb2f0e035a0e3d01  10.17.174.30    INFO    dc=openam,dc=forgerock,dc=org   "cn=dsameuser,ou=DSAME Users,dc=openam,dc=forgerock,dc=org" AUTHENTICATION-100  DataStore   "Not Available" 10.17.174.30

文件B:

"2014-02-26 16:02:27"   "Login Failed"  dennis  "Not Available" 10.17.174.30    INFO    dc=openam,dc=forgerock,dc=org   "cn=dsameuser,ou=DSAME Users,dc=openam,dc=forgerock,dc=org" AUTHENTICATION-200  DataStore   "Not Available" 10.17.174.30    
"2014-02-26 16:02:37"   "Login Failed"  purva   "Not Available" 10.17.174.30    INFO    dc=openam,dc=forgerock,dc=org   "cn=dsameuser,ou=DSAME Users,dc=openam,dc=forgerock,dc=org" AUTHENTICATION-200  DataStore   "Not Available" 10.17.174.30

我需要合并文件(非常标准的文件),但是我必须根据第1列中的时间将行插入最终文件中.我还需要为每行修改其他几项,但是我敢肯定,我可以确定那出来.我对基于时间列的排序感到困惑.

I need to merge the files (pretty standard) but I have to insert the rows into final file based on time found in column 1. I have several other items to modify for each line but I'm pretty sure I can figure that out. The sorting based on time column has me stumped.

因此,在这种情况下,我将有一个文件,文件A的行结尾.

So in this case I would have a file with the line from File A at the end.

其他详细信息.

只是为了刷新自己的语言,我正在分析第一个文件.这是我到目前为止的内容:

Just to refresh myself on gawk I was working on parsing the first file. Here is what I have so far:

#!/bin/awk -f
BEGIN {
    FS="\t";
}
{
    # if we have more than 12 fields for the current row, proceed
    if ( NF > 12 )
    {
        # start looking for the user name
        n = split( $3, var1, ",");
        if (n > 4)
        {
            n2 = split (var1[1], var2, "=");
            if (n2 >= 2)
            {
                # Ignore any line where we do not have "id=xxxxx,..."
                if (var2[1] == "id")
                {
                    print $1, "N/A", "N/A", $12, $5, $5, var2[2]
                }
            }
        }
    }
}
END {
    print "Total Number of records=" NR
}

由于要同时处理两个文件,我可能需要将其移到函数中以使其变得更容易.

I probably need to move that into a function to make it easier since I'm going to be processing two files at the same time.

推荐答案

基于linuxbash标记,您可以将两个文件串联起来,按第一个字段对它们进行排序,然后将awk命令应用于结果:

Based in the linux and bash tags, you can concatenate both files, sort them by first field and then apply your awk command to the result:

cat fileA fileB | sort -t$'\t' -s -k1,1 | awk -f script.awk

这篇关于AWK是否可以读取时间字段并将其用于排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆