比较多个文件行两列,匹配第一个比较第二个

比较多个文件行两列,匹配第一个比较第二个

我有两个这样的输入文件:

文件1

ABC001;text;text;5.00;text;text;;20/06/2020;http://www.domain.com/img/foobar4325.jpg
ABC002;text;text;15.20;text;text;;4/12/2021;http://www.domain.com/img/foobar545.jpg
ABC003;text;text;10.00;text;text;;24/07/2021;http://www.domain.com/img/foobar6y6.jpg
ABC004;text;text;4.90;text;text;;31/12/2021;http://www.domain.com/img/foobar5464.jpg
ABC007;text;text;10.30;text;text;;3/12/2021;http://www.domain.com/img/foobar45tgv.jpg
ABC010;text;text;9.00;text;text;;20/12/2021;http://www.domain.com/img/foobar2345f.jpg

file2(“四舍五入”价格不含 .00)

ABC001;text;text;6
ABC002;text;text;15.20
ABC003;text;text;10
ABC004;text;text;5.50
ABC005;text;text;25
ABC007;text;text;10.50
ABC010;text;text;9

所需输出:

ABC001;text;text;5.00;text;text;;20/06/2020;http://www.domain.com/img/foobar4325.jpg
ABC004;text;text;4.90;text;text;;31/12/2021;http://www.domain.com/img/foobar5464.jpg
ABC007;text;text;10.30;text;text;;3/12/2021;http://www.domain.com/img/foobar45tgv.jpg

这些行需要匹配第一列,然后比较匹配的行“价格”列(第五),如果价格在数字上不同,我只想从 file1 中提取行。

我用这个(GNU Awk 4.0.2):

awk -F';' -v RS='[\r\n]+' 'FNR==NR{righe[$1]; next} $1 in righe' file1.csv file2.csv > output.csv

比较两个 csv 文件,但我无法添加价格条件

答案1

您只需要选择正确的列并首先阅读 then file2file1此外您还需要比较值部分和关键部分:

awk -F';' 'NR==FNR{ id[$1]=$4; next} ($1 in id) && id[$1]!=$4' file2 file1

这里$1用于数组的键ID$4是价值部分。

相关内容