-1

I am looking for advance two file compare shell/bash/php. Let say i have such files:

file1

.run file=test_script.sql rev=1.1
.run file=test_sql.sql rev=1.1
.run file=test_drop.sql rev=1.2

file2

.run file=test_drop.sql rev=1.2
.run file=test_grant.sql rev=1.1
.run file=test_script.sql rev=1.2

get the difference between those files (ignoring line order) that is

.run file=test_grant.sql rev=1.1 #(because new line wasn't in file1 at all)
.run file=test_script.sql rev=1.2 #(because rev changed from rev=1.1 to rev=1.2)

but that is not all, i want to check if there was same (.run file=name) in the old file and if it was then get it's (rev=number). So that the final output will look like this:

file3:

 test_grant.sql 1.1 1.1
 test_script.sql 1.1 1.2

so far: fgrep -x -v -f file1 file2

gets

.run file=test_grant.sql rev=1.1
.run file=test_script.sql rev=1.2
4

1 回答 1

1

这个awk脚本应该做你想做的事:

awk 'NR==FNR {
    map[$2]=$3
    next;
}

!map[$2] || (map[$2] != $3) {
    sub3=substr($3, index($3,"=")+1)
    subm2=substr(map[$2], index(map[$2],"=")+1)
    print substr($2, index($2,"=")+1), subm2?subm2:sub3, sub3
}' file1 file2

在查看第一个文件 ( NR==FNR)时,将rev字段存储在键map下的数组中file

在查看第二个文件(第二个块)时,如果file此行中的字段不在map数组中或当前rev字段与匹配字段不匹配,rev则打印当前行。

要处理已删除的行,您需要{delete map[$2]}在第二个块之后添加,然后添加END {for (rev in map) {print "Missing: .run "map[rev]" "rev}}到末尾。

于 2016-01-28T17:36:57.407 回答