
ファイル1
products total
apple 6
yam 5
fish 6
meat 3
ファイル2
products total
apple 6
yam 7
fish 3
meat 5
私がしたいこと。両方のファイルの内容を比較するスクリプトが必要です。ファイル1の製品とファイルの製品を一致させ、合計を比較する必要があります。ファイル1の合計がファイル2の合計よりも大きい場合は、一部の内容を表示する必要があります。他のものが表示されないようにする場合
私が期待していたこと
file 1 file 2 the output of the script
products total products total
apple 6 apple 6 they are equal
yam 5 yam 7 file 2 is more
fish 6 fish 3 file 1 is more
meat 3 meat 5 file 2 is more
答え1
あなたの質問を正しく理解した場合:
ファイルがタブで区切られていると仮定し、ループを使用して要素のすべての値を保存し、2つのループを繰り返し比較します。
prods1=() #product names in file 1
tots1=() #totals in file 1
prods2=() #product names in file 2
tots2=() #totals in file 2
#read in the stored values
while IFS=\t read -r p t; do
prods1+=("$p")
tots1+=("$t")
done <<< file1
while IFS=\t read -r p t; do
prods2+=("$p")
tots2+=("$t")
done <<< file2
output=() #will store your output
for((i=0; i<${#tots1[@]};++i)); #you can set i to 1 if you want to skip the headers
do
#check if the values are equivalent
if ((${tots1[i]} == ${tots2[i]}));
output+="They are equal"
#ADD THE REST OF THE COMPARISONS HERE
fi
done
その後、配列にはoutput
必要な出力が含まれ、コンソールまたは任意の形式のファイルに印刷できます。 :)