我想根據另一個文件的順序對兩列文件進行排序。使用以下命令:bash shell腳本中的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
當我在外殼上使用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
...
T他的問題出現時,我嘗試把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
有人能告訴我這裏錯了嗎?
您是否嘗試設置文件的完整路徑? –
使用編輯器的{}}按鈕編輯您的問題,以正確地格式化您的代碼,以便我們知道您的代碼的真實外觀。空白區域在awk腳本中很重要。 –
@James:是的,我也嘗試了完整的路徑。仍然是同樣的問題。 – samira