簡體   English   中英

如何在shell腳本中將awk的輸出獲取到數組中

[英]how to get output of awk into array in shell script

我只是比較兩個文件,並顯示不相似的行

echo $(awk 'FNR==NR{f[$0]+=1; next} !($0 in f)' $file1 $file2)

我能夠得到這些價值。

當我嘗試通過傳遞給array來執行相同操作時,出現“:command not found”錯誤

declare -a myarr=()
myarr=$("$(awk 'FNR==NR{f[$0]+=1; next} !($0 in f)' $file1 $file2 )")

請幫助,在此先感謝。

刪除報價加上前導美元:

myarr=$("$(awk 'FNR==NR{f[$0]+=1; next} !($0 in f)' $file1 $file2 )")

應該

myarr=($(awk 'FNR==NR{f[$0]+=1; next} !($0 in f)' $file1 $file2))

校驗:


順便說一句,您的awk命令不會打印file1獨有的行。 可能是您想要的comm命令,但是請注意comm需要排序的輸入:

comm -13 <(sort "${file1}") <(sort "${file2}")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM