Felipe Alvarez
Felipe Alvarez

Reputation: 3908

Sort values in array with awk

This script works fine for me. It is able to email the owner of one or more locks and show him/her the list of locks s/he is holding.

However, I would like the file paths to be sorted alphanumerically. I tried using print owner | "sort" but the result looks strange.

I pass this into the script: 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"
    }
}

Sample Input

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):

Desired Output

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

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

and so on.

These are going to be emailed to the users by (for example) piping to mutt as you can see near the bottom of my sample script above.

Upvotes: 1

Views: 116

Answers (1)

glenn jackman
glenn jackman

Reputation: 246827

You're just not closing the coprocess: it needs to be signalled that it has all the input it's going to get:

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

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

Upvotes: 2

Related Questions