Tamoghna Majumdar
Tamoghna Majumdar

Reputation: 49

Passing AWK variable value to nested AWK

Is it possible to pass an AWK variable within another AWK block in the same inline? Will the Below code Work Where $8 is having value Mon Nov 16 16:00:00 2015

|awk -F"[][]" '{print $2, $6, ${echo $8|awk '{d=$0;cmd="date -d\""d"\" +\"%a %b %d %r %Y\"" ;cmd|getline $0;close(cmd)}7'}}'

Upvotes: 2

Views: 887

Answers (1)

F. Knorr
F. Knorr

Reputation: 3065

Based on the two questions that you have asked today, this question actually asks to combine the two answers in one statement.
This can be achieved easier (i.e., without an external call) by modifying Ed Mortons answer to Date manipulation in awk variable : Non Military Format and embedding it into your awk script:

awk -F"[][]" '{ split($8,a,/[: ]/)
    p = "AM"
    if(a[4]a[5]a[6] > 120000) {
        p = "PM"
        a[4] = a[4] > 12 ? a[4] - 12 : a[4]
    } else if(a[4] < 1) {
        a[4] = 12
    }
    datetime=sprintf("%02d:%02d:%02d %s",a[4],a[5],a[6],p)
    print $2, $6, a[1]" "a[2]" "a[3]" "datetime" "a[7]}'

Upvotes: 1

Related Questions