bash shell脚本中的awk错误

时间:2017-03-05 17:32:45

标签: bash shell unix awk

我正在尝试根据另一个文件的顺序对两列文件进行排序。使用以下命令:

  awk  'FNR == NR {lineno[$1] = NR; next} {print lineno[$1], $0;}'  associativity4Way_cacheSize32KB_replacementPolicy1.outs  associativity4Way_cacheSize32KB_replacementPolicy2.outs   | sort -k  1,1n  | cut -d " " -f2- > sorted

当我在shell外部使用AWK命令时,它工作正常。以下是我的文件的主要内容和输出:

这是“associativity4Way_cacheSize32KB_replacementPolicy2.outs”的负责人

LONG_MOBILE-10.bt9.trace.gz_a4_b64_c32  4.516192
LONG_MOBILE-10.bt9.trace.gz_a4_b64_c32  4.147467
LONG_MOBILE-10.bt9.trace.gz_a4_b64_c64  2.040121
LONG_MOBILE-10.bt9.trace.gz_a4_b64_c64  1.837639
LONG_MOBILE-10.bt9.trace.gz_a4_b64_c64  3.068701
LONG_MOBILE-11.bt9.trace.gz_a4_b64_c32  0.474358
LONG_MOBILE-11.bt9.trace.gz_a4_b64_c32  0.545525
LONG_MOBILE-11.bt9.trace.gz_a4_b64_c32  0.469907
LONG_MOBILE-11.bt9.trace.gz_a4_b64_c64  0.19461
...

这是“ssociativity4Way_cacheSize32KB_replacementPolicy1.outs”

SHORT_SERVER-2.bt9.trace.gz_a4_b64_c32  29.869599
SHORT_SERVER-2.bt9.trace.gz_a4_b64_c32  29.100611
SHORT_SERVER-2.bt9.trace.gz_a4_b64_c32  29.068284
SHORT_SERVER-2.bt9.trace.gz_a4_b64_c32  28.559002
SHORT_MOBILE-20.bt9.trace.gz_a4_b64_c32 21.332859
SHORT_MOBILE-22.bt9.trace.gz_a4_b64_c32 20.605805
SHORT_MOBILE-20.bt9.trace.gz_a4_b64_c32 20.256246
SHORT_MOBILE-20.bt9.trace.gz_a4_b64_c32 20.193713
SHORT_MOBILE-22.bt9.trace.gz_a4_b64_c32 20.119883
SHORT_MOBILE-22.bt9.trace.gz_a4_b64_c32 20.099358
...

输出完全按照我的意愿排序:

SHORT_SERVER-2.bt9.trace.gz_a4_b64_c32  28.559002
SHORT_SERVER-2.bt9.trace.gz_a4_b64_c32  29.068284
SHORT_SERVER-2.bt9.trace.gz_a4_b64_c32  29.100611
SHORT_SERVER-2.bt9.trace.gz_a4_b64_c32  29.869599
SHORT_MOBILE-20.bt9.trace.gz_a4_b64_c32 20.193713
SHORT_MOBILE-20.bt9.trace.gz_a4_b64_c32 20.256246
SHORT_MOBILE-20.bt9.trace.gz_a4_b64_c32 21.332859
...

当我尝试将awk命令放入我的bash脚本中以便能够为其他文件执行时,会出现问题:

set lru = "associativity${a}Way_cacheSize${c}KB_replacementPolicy1.outs"


set policy = "associativity${a}Way_cacheSize${c}KB_replacementPolicy${r}.outs"


awk  "FNR == NR {lineno["$policy"] = NR; next} {print lineno["$policy"], "$lru";}" "$lru" "$policy" | sort -k 1,1n  | cut -d " " -f2- > "${policy}_sorted"

这是我得到的错误:

awk: cmd. line:1: FNR == NR {lineno[associativity4Way_cacheSize32KB_replacementPolicy3.outs] = NR; next} {print lineno[associativity4Way_cacheSize32KB_replacementPolicy3.outs], associativity4Way_cacheSize32KB_replacementPolicy1.outs;}
awk: cmd. line:1:                                                                                                                                                                                                                  ^ syntax error

我也试过这个(这里有一个答案建议),但输出不正确。它在输出文件中多次打印'lru'文件的名称。

set lru = "associativity${a}Way_cacheSize${c}KB_replacementPolicy1.outs"
            set policy = "associativity${a}Way_cacheSize${c}KB_replacementPolicy${r}.outs"
            awk -v policy="$policy" -v lru="$lru" 'FNR == NR {lineno[policy] = NR; next} {print lineno[policy], lru}' "$lru" "$policy"  | sort -k 1,1n  | cut -d ' ' -f2- > "${policy}_sorted"


 associativity8Way_cacheSize64KB_replacementPolicy1.outs
associativity8Way_cacheSize64KB_replacementPolicy1.outs
associativity8Way_cacheSize64KB_replacementPolicy1.outs
associativity8Way_cacheSize64KB_replacementPolicy1.outs
associativity8Way_cacheSize64KB_replacementPolicy1.outs
associativity8Way_cacheSize64KB_replacementPolicy1.outs
associativity8Way_cacheSize64KB_replacementPolicy1.outs
associativity8Way_cacheSize64KB_replacementPolicy1.outs
associativity8Way_cacheSize64KB_replacementPolicy1.outs
associativity8Way_cacheSize64KB_replacementPolicy1.outs

有人可以告诉我错在哪里吗?

1 个答案:

答案 0 :(得分:2)

永远不要用双引号括起任何脚本(awk sed,无论如何),因为它只会引入复杂性和脆弱性。

改变这个:

const result = _.map(grouped, (value, date) => ({
    date: date,
    products: sumProductsUnits(
        _.flatMap(value, el => el.products)
    )
}));

到此:

[
   {
      "date":"date",
      "products":[
         {
            "product":"Apple",
            "units":5
         },
         {
            "product":"Pear",
            "units":4
         }
      ]
   }
]

看看它是怎么回事。获得由Arnold Robbins撰写的Effective Awk Programming,第4版,开始学习awk。