johannes
johannes

Reputation: 31

Need some assistance from good bash programmers

I have this script below

#!/bin/bash
function labels2 () {
    awk '
    /[0-9]/{
    print substr($3,length($3)-11), $3
    }' $@ | /bin/sort -u  | awk '{print "BUILD: " NR, $2}'
}

function labels () {
    awk '
    /[0-9]/{
    BL[$3] = substr($3,length($3)-11)
    }
    END {
    asort(BL)
    for (i in BL) {
        print i, BL[i]
    }
    }' $@
}


labels $@

for a in $@
do
    labels $@ | gawk '
    /BUILD:/ {
    BUILD[$2] = $3
    BUILDCNT ++
    next
    }
    /[0-9]/ {
    DATEd[$3] = $1
    TIMEd[$3] = $2
    MODULESd[$3] = $4
    CASESd[$3] = $5
    FAILEDd[$3] = $6
    COVERd[$3] = $7
    LOCd[$3] = $8
    }
    END {
    SUBSYSTEM=substr(FILENAME, 1, length(FILENAME)-7)
    LABEL= "\"" toupper(SUBSYSTEM) "\""

    print  "#{"
        print "\"buildnames\": {"
        print "        \"label\": \"buildnames\","
        print "        \"data\": ["

        print "        ]"
        print " }"
        print "};"
        print "#{"
        print "\"subsystem\": " LABEL ","
        print "    \"date\": {"
        print "        \"label\": " LABEL ","
        print "        \"data\": ["
        for (i = 0 ; i <= BUILDCNT; i ++ ) {
            B=BUILD[i]
            if (DATEd[B]) { print "            [" i ", \"" DATEd[B] "\"],"}
        }
        print "        ]"
        print "    },"
        print " \"time\" : {"
        print "        \"label\": " LABEL ","
        print "        \"data\": ["
        for (i = 0 ; i <= BUILDCNT; i ++ ) {
            B=BUILD[i]
            if (TIMEd[B]) { print "            [" i ", \"" TIMEd[B] "\"],"}
        }
        print "        ]"
        print "    },"
        print " \"modules\" : {"
        print "        \"label\": " LABEL ","
        print "        \"data\": ["
        for (i = 0 ; i <= BUILDCNT; i ++ ) {
            B=BUILD[i]
            if (MODULESd[B]) { print "            [" i ", \"" MODULESd[B] "\"],"}
        }
        print "        ]"
        print "    },"
        print " \"cases\" : {"
        print "        \"label\": " LABEL ","
        print "        \"data\": ["
        for (i = 0 ; i <= BUILDCNT; i ++ ) {
            B=BUILD[i]
            if (MODULESd[B]) { print "            [" i ", \"" MODULESd[B] "\"],"}
        }
        print "        ]"
        print "    },"
        print " \"failed\" : {"
        print "        \"label\": " LABEL ","
        print "        \"data\": ["
        for (i = 0 ; i <= BUILDCNT; i ++ ) {
            B=BUILD[i]
            if (FAILEDd[B]) { print "            [" i ", \"" FAILEDd[B] "\"],"}
        }
        print "        ]"
        print "    },"
        print " \"cover\" : {"
        print "        \"label\": " LABEL ","
        print "        \"data\": ["
        for (i = 0 ; i <= BUILDCNT; i ++ ) {
            B=BUILD[i]
            if (COVERd[B]) { print "            [" i ", \"" COVERd[B] "\"],"}
        }
        print "        ]"
        print "    },"
        print " \"loc\" : {"
        print "        \"label\": " LABEL ","
        print "        \"data\": ["
        for (i = 0 ; i <= BUILDCNT; i ++ ) {
            B=BUILD[i]
            if (LOCd[B]) { print "            [" i ", \"" LOCd[B] "\"],"}
        }
        print "        ]"
        print "    }"
        print "    };"
    }
    ' - $a
done

And it gives the following output when "feeded" with the current textfile gps.txt

2011-01-22 22:12 P16A22_110114072915 22 1312 75 13.55 1399
_




1 110114072915
#{
"buildnames": {
        "label": "buildnames",
        "data": [
        ]
        }
};
#{
"subsystem": "GPS",
    "date": {
        "label": "GPS",
        "data": [
            [0, "1"],
        ]
    },
        "time" : {
        "label": "GPS",
        "data": [
            [0, "110114072915"],
        ]
    },
        "modules" : {
        "label": "GPS",
        "data": [
        ]
    },
        "cases" : {
        "label": "GPS",
        "data": [
        ]
    },
        "failed" : {
        "label": "GPS",
        "data": [
        ]
    },
        "cover" : {
        "label": "GPS",
        "data": [
        ]
    },
        "loc" : {
        "label": "GPS",
        "data": [
        ]
    }
    };

What I want to look like is this

#{
"buildnames": {
        "label": "buildnames",
        "data": [[0,"BUILD: 1 P16A22_110114072915"]
        ]
        }
};
#{
"subsystem": "GPS",
    "date": {
        "label": "GPS",
        "data": [
            [0, "2011-01-22"],
        ]
    },
        "time" : {
        "label": "GPS",
        "data": [
            [0, "22:12"],
        ]
    },
        "modules" : {
        "label": "GPS",
        "data": [[0,22]
        ]
    },
        "cases" : {
        "label": "GPS",
        "data": [[0,1312]
        ]
    },
        "failed" : {
        "label": "GPS",
        "data": [[0,75]
        ]
    },
        "cover" : {
        "label": "GPS",
        "data": [[0,13.55]
        ]
    },
        "loc" : {
        "label": "GPS",
        "data": [[0,1399]
        ]
    }
    };

So to split up this problems into subproblems

  1. Why is the row 1 110114072915 at top, and how can I remove it?

  2. How can I use the function labels2() so that that the output from it (BUILD: 1 P16A22_110114072915) gets in the buildnames like this

    "buildnames": {
        "label": "buildnames",
        "data": [[0,"**BUILD: 1 P16A22_110114072915**"]
        ]
        }
    
  3. This is the last question: Why don't this work MODULESd[$3] = $4, CASESd[$3] = $5 etc... when I loop through it and print it out, it becomes nothing.

Please note here that the textfile gps.txt will be continuously extended, so that in the future it might be 50 rows, and it would be good if the sollution still would work =)

I know that this became alot of text and I apologize for that, and I might not get answers on all my questions, but if you might have an Idea how to acchieve any of those I would be more than happy, because I'm currently stuck and don't know how to do what I want to do. And unfortunately this way of doing it is my only option.

Thanks you for your efforts =)

Upvotes: 1

Views: 211

Answers (2)

Jonathan Wakely
Jonathan Wakely

Reputation: 171293

1) Why is the row 1 110114072915 at top, and how can I remove it?

Foo Bah correctly answered this question.

This looks wrong to me:

for a in $@
do
labels $@ | gawk '

Why are you looping over all the args, then running labels $@ not labels $a?

2) How can I use the function labels2()

I would run the function and assign the output to an awk variable:

labels $@ | gawk -v "bnames=\"$(labels $a)\"" '

Then use that variable in the awk script:

    print "        \"data\": [", bnames

3) This is the last question: Why don't this work

I don't know, but you'll never figure it out unless you break the problem down into smaller pieces and debug it yourself. If you write a hundred line awk script and it doesn't work, the answer is to try getting a simpler ten line version to work, then grow it piece-by-piece and check each part works.

Upvotes: 0

Foo Bah
Foo Bah

Reputation: 26271

This is all based on mental processing -- I havent actually run the commands yet. It would help to see a sample of that text file

1) Why is the row 1 110114072915 at top, and how can I remove it?

It looks like the bare labels $@ call before the loop is printing the info. Get rid of that.

2) You probably should migrate that logic into the bottom awk command. The associative array assignment can be done in the second block /[0-9]/ and the END logic can be put directly in the END block of the large script.

3) does the text file have 8 fields per line? As a debug, use something like print NF > "/dev/stderr"; to make sure that, when the variables are being set, that there are indeed 8 fields.

Upvotes: 1

Related Questions