使用awk对数组中的值进行排序

时间:2014-06-17 00:16:49

标签: awk gawk

这个脚本对我来说很好。它可以通过电子邮件发送一个或多个锁的所有者,并向他/她显示他/她持有的锁列表。

但是,我希望文件路径按字母顺序排序。我尝试使用print owner | "sort",但结果看起来很奇怪。

我将其传递给脚本:svnadmin lslocks $SVN_REPO /Trunk | locks.awk

#!/bin/gawk
BEGIN {
    # RS="" means "record separator is a blank line"
    RS=""
    FS="\n"
    counter=0
    # number of days (seconds) for an old lock
    THRESHOLD_DAYS      =   7
    THRESHOLD_SECONDS   =   60 * 60 * 24 * THRESHOLD_DAYS
    # seconds of right now
    NOW_SECONDS         =   systime()
}

# This code is processed FOR EACH line of input
{
    FILE_PATH       =   gensub(/Path: ([[:print:]]+).*/, "\\1", "g", $1)
    #UUID_TOKEN      =   $2
    OWNER           =   gensub(/Owner: ([[:alnum:]]+).*/, "\\1", "g", $3)
    #LOCK_CREATED    =   $4
    #EXPIRES         =   $5
    #COMMENT         =   $6

    # skip if owner matches regex
    if (OWNER ~ /kerri|jon|brian|andy|steve|andrew|matthew.nolan|devel|wayne|matty/ ||
        FILE_PATH ~ /UFT\//){
        next
    }

    # get only the timestamp
    # e.g. 2014-04-14 14:09:10
    LOCK_STAMP      =   gensub(/Created: ([[:graph:]]+ [[:graph:]]+) .*/, "\\1", "g", LOCK_CREATED)

    # mktime expected syntax: "YYYY MM DD HH MM SS [DST]"
    LOCK_SECONDS    =   gensub(/[-:]/," ","g", LOCK_STAMP)

    # if NOW minus LOCK is greater than THRESHOLD :: the lock is old
    if ( (NOW_SECONDS - LOCK_SECONDS) > THRESHOLD_SECONDS ){
        # use the spaces character for string concatenation
        # LOCKS[OWNER]    =   LOCKS[OWNER] "\n\t" FILE_PATH
        LOCKS[OWNER]    =   FILE_PATH "\n" LOCKS[OWNER]
    }

    ++counter
}

END {
    for (i in LOCKS){
        print i
        printf "%s", LOCKS[i] | "sort"
        # MESSAGE = i_OWNER ": you are holding locks on the following files:" LOCKS[i_OWNER]
        # print MESSAGE | "mutt -s '" i_OWNER ": You have old locks in SVN repository' " i_OWNER "@example.com.au"
    }
}

样本输入

Path: /Trunk/Lettus/UFT/30-order-and-invoice/default.cfg
UUID Token: opaquelocktoken:703b9c76-a0c6-4ecd-9078-878382e03572
Owner: matty
Created: 2014-05-22 14:36:14 +1000 (Thu, 22 May 2014)
Expires:
Comment (1 line):


Path: /Trunk/Scanner/Lettus_V2_IntermecVersion/LettusMobile/bin/Release/LettusMobile/Intermec.DataCollection.CF2.xml
UUID Token: opaquelocktoken:4fec94d3-4a19-4366-9efb-c814152a167b
Owner: felipe
Created: 2014-05-28 12:14:56 +1000 (Wed, 28 May 2014)
Expires:
Comment (1 line):


Path: /Trunk/Lettus/UFT/00-lettus-common/Action0/ObjectRepository.bdb
UUID Token: opaquelocktoken:bec7866a-2f39-4a6d-b2fd-4b47ef3cdece
Owner: matty
Created: 2014-05-22 14:36:15 +1000 (Thu, 22 May 2014)
Expires:
Comment (1 line):


Path: /Trunk/Lettus/UFT/30-order-and-invoice/Action2/Resource.mtr
UUID Token: opaquelocktoken:0d1234a3-8a1f-434e-91e8-03315e64b085
Owner: matty
Created: 2014-05-22 14:36:17 +1000 (Thu, 22 May 2014)
Expires:
Comment (1 line):


Path: /Trunk/Lettus/UFT/30-order-and-invoice/Default.xls
UUID Token: opaquelocktoken:28f63d50-3280-4f90-b552-5f297ab8c973
Owner: matty
Created: 2014-05-22 14:36:15 +1000 (Thu, 22 May 2014)
Expires:
Comment (1 line):


Path: /Documents/Software/JDeveloper/oneNote/JDeveloperNotebook/Things to Remember.one
UUID Token: opaquelocktoken:5236cfdf-ab7e-4336-a7b8-98e6db221286
Owner: wayne
Created: 2014-03-14 14:10:40 +1000 (Fri, 14 Mar 2014)
Expires:
Comment (1 line):

期望输出

wayne
/Trunk/aaa
/Trunk/file1
/Trunk/file2
/Trunk/zzz

matty
/Trunk/bbb
/Trunk/file3
/Trunk/file4
/Trunk/zzzzzz

等等。

这些将通过(例如)管道发送给用户通过电子邮件发送给mutt,您可以在我上面的示例脚本底部附近看到。

1 个答案:

答案 0 :(得分:2)

你只是没有关闭协同处理:它需要发出信号表明它有它将获得的所有输入:

for (owner in LOCKS){
    print owner
    printf "%s", LOCKS[owner] | "sort"
    close("sort")
}

http://www.gnu.org/software/gawk/manual/html_node/Redirection.html
http://www.gnu.org/software/gawk/manual/html_node/Close-Files-And-Pipes.html