从两个文件 shell 脚本中添加值



>我有 2 个文件,其中包含以下格式的数据。 文件1.txt

abc , 234
bcd , 457
xyz , 123

文件2.txt

abc , 23
bcd , 458

现在,我正在寻找以下:-

abc , 211 ( diff of file1 and file2)
bcd, -1

sum of file1 values present in file2    =  691 (abc count + bcd count in file 1 )
sum of file1 values present in file2    =  481 (abc count + bcd count in file 2 ) 

我知道使用 grep -f 我们可以合并这两个文件,但如何进行加法和减法我不确定。

任何帮助都会得到很多赞赏。

问候。

默认情况下,join命令将使用第一个字段作为公用键来联接两个文件。

然后减法可以通过壳算术展开来完成。

join -t , File1.txt File2.txt |
while IFS="$IFS," read -r key val1 val2; do
printf '%s , %sn' "$key" "$((val1-val2))"
done

最新更新