awk:比较2列的2个文件

时间:2019-04-18 15:13:02

标签: awk

我必须使用awk比较2个文件。         每个文件的结构相同:路径校验和

File1.txt

/content/cr444/commun/      50d174f143d115b2d12d09c152a2ca59be7fbb91
/content/cr764/commun/     10d174f14fd115b2d12d09c152a2ca59be7fbb91
/content/cr999/commun/     10d174f14fd115b2d12d09c152a2ca59be7fbbpp

File2.txt

/content/cr555/test/        51d174f14f6115b2d12d09c152a2ca59be7fbb91
/content/cr764/commun/     10d174f14fd115b2d12d09c152a2ca59be7fbb78
/content/cr999/commun/     10d174f14fd115b2d12d09c152a2ca59be7fbbpp

预期结果是.csv(带有分隔符|):

/content/cr444/commun/|50d174f143d115b2d12d09c152a2ca59be7fbb91||not in file2
/content/cr555/test/||51d174f14f6115b2d12d09c152a2ca59be7fbb91|not in file1 
/content/cr999/commun/|10d174f14fd115b2d12d09c152a2ca59be7fbbpp|10d174f14fd115b2d12d09c152a2ca59be7fbbpp|same checksum
/content/cr764/commun||10d174f14fd115b2d12d09c152a2ca59be7fbb91|10d174f14fd115b2d12d09c152a2ca59be7fbb78|not same checksum

2 个答案:

答案 0 :(得分:2)

我认为输出线的顺序并不重要。然后您可以:

  1. * { margin: 0; } .wrapper1 { display: grid; grid-template-columns: 2fr 2fr; border: 2px solid #6cccb0; height: 30px; background-color: #6cccb0; font-size: 1em; color: orange; } @media only screen and (max-width: 440px) { .wrapper1 { font-size: 0.4em; color: yellow; } } @media only screen and (min-width: 440px) { .wrapper1 { font-size: 0.5em; color: aqua; } } @media only screen and (min-width: 560px) { .wrapper1 { font-size: 0.6em; color: green; } } @media only screen and (min-width: 728px) { .wrapper1 { font-size: 0.8em; color: blue; } } @media only screen and (min-width: 1024px) { .wrapper1 { font-size: 1em; color: red; } } 中的行收集到索引数组(File1.txt
  2. 来自$1 -> $2的生产线:
    1. 如果File2.txt在(1)的索引数组中,比较它们的校验和并进行相应打印
    2. 如果$1不在(1)的索引数组中,则进行相应打印
  3. 从数组(1)打印所有剩余的主题

代码如下:

$1

输出:

$ awk 'BEGIN{OFS="|"} NR==FNR{f1[$1]=$2; next} {if ($1 in f1) { print $1,f1[$1],$2,($2==f1[$1]?"":"not ")"same checksum"; delete f1[$1]} else print $1,"",$2,"not in file1"} END{for (i in f1) print i,f1[i],"","not in file2"}' File1.txt File2.txt

答案 1 :(得分:0)

一种方法,使用join合并两个文件,然后awk比较每一行的校验和:

$ join -a1 -a2 -11 -21 -e XXXX -o 0,1.2,2.2 <(sort -k1 file1.txt) <(sort -k1 file2.txt) |
   awk -v OFS='|' '$2 == "XXXX" { print $1, "", $3, "not in file1"; next }
                   $3 == "XXXX" { print $1, $2, "", "not in file2"; next }
                   $2 == $3 { print $1, $2, $3, "same checksum"; next }
                   { print $1, $2, $3, "not same checksum" }'
/content/cr444/commun/|50d174f143d115b2d12d09c152a2ca59be7fbb91||not in file2
/content/cr555/test/||51d174f14f6115b2d12d09c152a2ca59be7fbb91|not in file1
/content/cr764/commun/|10d174f14fd115b2d12d09c152a2ca59be7fbb91|10d174f14fd115b2d12d09c152a2ca59be7fbb78|not same checksum
/content/cr999/commun/|10d174f14fd115b2d12d09c152a2ca59be7fbbpp|10d174f14fd115b2d12d09c152a2ca59be7fbbpp|same checksum