使用shell中的awk读取文件内容

时间:2014-08-04 22:56:25

标签: shell

这是我的输入

<mydata>
    <parent detail="school1">
        <CHILD attribute="0">0</CHILD>
        <CHILD attribute="1">1932</CHILD>
        <CHILD attribute="2">0</CHILD>
        <CHILD attribute="3">500</CHILD>
        <CHILD attribute="4">0</CHILD>
        <CHILD attribute="5">0</CHILD>
        <CHILD attribute="6">7819</CHILD>
        <CHILD attribute="7">0</CHILD>
        <CHILD attribute="8">299</CHILD>
        <CHILD attribute="9">0</CHILD>
    </parent>
    <parent detail="school2">
        <CHILD attribute="0">1</CHILD>
        <CHILD attribute="1">7000</CHILD>
        <CHILD attribute="2">0</CHILD>
        <CHILD attribute="3">0</CHILD>
        <CHILD attribute="4">600</CHILD>
        <CHILD attribute="5">0</CHILD>
        <CHILD attribute="6">11674</CHILD>
        <CHILD attribute="7">0</CHILD>
        <CHILD attribute="8">489</CHILD>
        <CHILD attribute="9">0</CHILD>
    </parent>
</mydata>

我当前的代码

sed 's|><|>\n<|g' $WORKING_PATH/mydatafile.log |
awk -F'"|<|>' '/parent detail/{p=$3}
               /CHILD attribute/{att=$3;val=$5;
                     if(val>100)print  "child value on " p, "attribute "att,"is at value: "val ,"\n"}'

我当前的输出

child value on school1 attribute 1 is at value 1932 
child value on school1 attribute 3 is at value 500
...
... 
child value on school2 attribute 1 is at value 7000 
child value on school2 attribute 4 is at value 600
...
...

现在我的要求是将以下属性值作为参数传递给if条件

我的文件内容是

attribute0=100
attribute1=60
attribute3=80
attribute4=90
attribute5=100
attribute6=90
attribute7=50
attribute8=80
attribute9=70

我需要将这些值作为动态参数传递给该条件并将结果打印为

child value on school1 attribute 1 is at value 1932 and threshold is 60
child value on school1 attribute 3 is at value 500 and threshold is 80

2 个答案:

答案 0 :(得分:1)

假设您的包含属性名称和阈值的文件名为thresholds,并且您无法以其他格式生成该文件。为方便处理,您需要删除单词&#39;属性&#39;从中。你需要&#39; =&#39;也是一个字段分隔符。

处理第一个文件的标准技巧与其他文件不同的是:

awk 'FNR == NR { …process first file… }
     FNR != NR { …process other files… }'

您还可以在第一个文件处理的操作中使用; next,这样您就不需要FNR != NR条件;它在这里可能很有用。请注意使用完整正则表达式将字段分隔符指定为awk;它说&#39;一个或多个双引号,小于,大于,管道或相等构成一个字段分隔符。

sed 's|><|>\n<|g' $WORKING_PATH/mydatafile.log |
awk -F '["<>|=]+' '
    FNR == NR         { gsub(/attribute/, "", $1); level[$1] = $2; next }
    /parent detail/   { p = $3; }
    /CHILD attribute/ { att = $3; val = $4;
                        if (att in level && level[att] < val)
                        {
                          printf "child value on %s attribute %d", p, att
                          printf " is at value %d and threshold is %d\n", val, level[att]
                        }
                      }
    ' thresholds -

-(个人短划线)参数表示“阅读标准输入”#。

(我不清楚为什么你在awk的字段分隔符列表中有管道符号,但我把它们中的一个留在了那里。)

对于样本数据,输出为:

child value on school1 attribute 1 is at value 1932 and threshold is 60
child value on school1 attribute 3 is at value 500 and threshold is 80
child value on school1 attribute 6 is at value 7819 and threshold is 90
child value on school1 attribute 8 is at value 299 and threshold is 80
child value on school2 attribute 1 is at value 7000 and threshold is 60
child value on school2 attribute 4 is at value 600 and threshold is 90
child value on school2 attribute 6 is at value 11674 and threshold is 90
child value on school2 attribute 8 is at value 489 and threshold is 80

使用相同的输出对GNU awk和Mac OS X(BSD)awk进行测试。

答案 1 :(得分:0)

如果我理解你想要实现的目标,我建议用SHELL在awk中加载这些参数,而不是用第一个awk命令加载。

一个例子:

. myFileContent.sh
awk -v attribute0=$attribute0 ' ... '