需要一些好的bash程序员的帮助

时间:2011-06-29 14:22:26

标签: bash awk

我在下面有这个脚本

#!/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

当使用当前文本文件gps.txt

“feeded”时,它会给出以下输出
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": [
        ]
    }
    };

我想看的是这个

#{
"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]
        ]
    }
    };

所以将这些问题分解为子问题

  1. 为什么行1 110114072915位于顶部,如何将其删除?

  2. 如何使用labels2()函数,以便它(BUILD: 1 P16A22_110114072915)的输出符合buildnames这样的

    "buildnames": {
        "label": "buildnames",
        "data": [[0,"**BUILD: 1 P16A22_110114072915**"]
        ]
        }
    
  3. 这是最后一个问题:为什么这不起作用MODULESd[$3] = $4CASESd[$3] = $5等......当我循环播放并打印出来时,它就变成了什么。

  4. 请注意这里将继续扩展文本文件gps.txt,以便将来可能会有50行,如果sollution仍然可以正常工作=)

    我知道这变成了很多文字,我为此道歉,我可能无法得到所有问题的答案,但如果你有一个想法如何取得任何一些我会更乐意,因为我我目前陷入困境,不知道该怎么做我想做的事情。不幸的是,这种做法是我唯一的选择。

    感谢您的努力=)

2 个答案:

答案 0 :(得分:1)

这都是基于心理处理 - 我还没有实际运行命令。查看该文本文件的示例

会有所帮助

1)为什么第1行110114072915位于顶部,如何将其删除?

在循环打印信息之前看起来像是labels $@调用。摆脱它。

2)您可能应该将该逻辑迁移到底部awk命令中。关联数组赋值可以在第二个块/[0-9]/中完成,END逻辑可以直接放在大脚本的END块中。

3)文本文件每行有8个字段吗?作为调试,使用print NF > "/dev/stderr";之类的东西来确保在设置变量时确实存在8个字段。

答案 1 :(得分:0)

  

1)为什么第1行110114072915位于顶部,如何将其删除?

Foo Bah正确回答了这个问题。

这对我来说不对:

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

为什么要循环遍历所有参数,然后运行labels $@而不是labels $a

  

2)我如何使用函数labels2()

我会运行该函数并将输出分配给awk变量:

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

然后在awk脚本中使用该变量:

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

3)这是最后一个问题:为什么不这样做

我不知道,但除非你把问题分解成小块并自己调试,否则你永远不会弄明白。如果你写了一百行awk脚本并且它不起作用,那么答案就是尝试让一个更简单的十行版本工作,然后逐个增长并检查每个部分的工作原理。