如果值 1 完全匹配,则匹配 2 个文件中的值 2

如果值 1 完全匹配,则匹配 2 个文件中的值 2

我有 2 个包含列表的文件。第 1 列是用户 ID,第 2 列是关联值

# cat file1
e3001 75
n5244 30
w1453 500

#cat file2
d1128 30
w1453 515
n5244 30
e3001 55

需要考虑的事情。

  1. 两个文件中的 userId 可能无法精确排序
  2. 文件中的 userId 数量可能有所不同

必需的

  • 首先,file1:column1 中的 userId 必须与 file2:column1 中的 UserId 匹配
  • 接下来将 file1:column2 中的值与 file2:column2 中的值进行比较
  • 打印值有方差的地方。还有额外的 userId(如果有)

输出:

e3001 has differnece, file1 value: 75 & file2 value: 55
w1453 has differnece, file1 value: 500 & file2 value: 515
d1128 is only present in filename: file1|file2

欢迎使用 1liner-awk 或 bash 循环的解决方案

我正在尝试循环,但它在吐垃圾,猜猜有一些逻辑错误

#!/usr/bin/env bash

## VARIABLES
FILE1=file1
FILE2=file2
USERID1=(`awk -F'\t' '{ print $1 }' ${FILE1}`)
USERID2=(`awk -F'\t' '{ print $1 }' ${FILE2}`)
USERDON1=(`awk -F'\t' '{ print $2 }' ${FILE1}`)
USERDON2=(`awk -F'\t' '{ print $2 }' ${FILE2}`)

for user in ${USERID1[@]}
do
    for (( i = 0; i < "${#USERID2[@]}"; i++ ))
    #for user in ${USERID2[@]}
    do
        if [[ ${USERID1[$user]} == ${USERID2[i]} ]]
        then
            echo ${USERID1[$user]} MATCHES BALANCE FROM ${FILE1}: ${USERDON1[$i]} WITH BALANCE FROM ${FILE2}: ${USERDON2[$i]}
        else
            echo ${USERID1[$user]} 
        fi
    done
done

下面是直接从 Linux 盒子复制的文件。它是制表符分隔的,但据我所知,awk 也可以使用制表符。

#cat file1
e3001   55
n5244   30
w1453   515

答案1

嗯 - 可以这么说,你的剧本走的是风景路线。一个简单的方法怎么样awk?喜欢

awk '
NR==FNR         {ARR[$1] = $2
                 F1      = FILENAME
                 next
                }
($1 in ARR)     {if ($2 != ARR[$1]) print $1 " has difference," \
                                          F1 " value: " ARR[$1] \
                                          " & " FILENAME " value: " $2 
                 delete ARR[$1]
                 next
                }
                {print $1 " is only present in filename: " FILENAME
                }
END             {for (a in ARR) print a " is only present in filename: " F1
                }
' file[12]
d1128 is only present in filename: file2
w1453 has difference, file1 value: 500 & file2 value: 515
e3001 has difference, file1 value: 75 & file2 value: 55

它将 file1 的所有内容读入一个数组,然后,对 file2 中的每一行检查$1数组索引,如果存在,则打印差异(如果没有,则不打印),并delete保存数组元素(delete可能是某些 awk 实现中缺少,顺便说一句)。如果不存在,请相应打印。在该END部分中,将打印所有剩余的数组元素,因为它们仅存在于 file1 中。

答案2

评论是不言自明的:

awk '
    BEGIN {file1 = ARGV[1]; file2 = ARGV[2]}

    # Load all file1 contents
    NR == FNR {map[$1] = $2; next}
    
    # If $1 is not in m then this key is unique to file2
    !($1 in map) {uniq[$1]; next}

    # If $1 is in m and the value differs there are delta
    # between the two files. Save it.
    $1 in map && map[$1] != $2 {diff[$1] = $2; next}

    # The two files have all the same data.
    {delete map[$1]}

    END {
        # Anything is in diff are in both files but
        # with different values
        for ( i in diff )
            print i, "has difference,", file1, "value:", map[i], "&", file2, "value:", diff[i]

        # Anything is still in m is only in file 1
        for ( i in map )
            if (!(i in diff))
                print i, "is only present in filename :", file1

        # Anything is in uniq is unique to file2
        for ( i in uniq )
            print i, "is only present in filename :", file2
    }
' file1 file2

答案3

对于这种事情来说,shell 是一个可怕的工具。另外,作为一般规则,您应该避免在 shell 脚本中对 shell 变量使用大写字母。由于按照惯例,全局环境 shell 变量都是大写的,因此这可能会导致命名冲突和难以调试的问题。最后,您的脚本需要单独读取该文件 4 次(!)并且然后处理数据。

话虽如此,这是另一种 awk 方法(坦率地说,鲁迪克的更好,但我已经写了这个,所以我还是要发布):

$ awk '{
  if(NR==FNR) {
    fn1=FILENAME;
    f1[$1]=$2;
    next
  }
  f2[$1]=$2;
  if($1 in f1){
    if($2 != f1[$1]){
      printf "%s is different; %s value: %s & %s value: %s\n", \
             $1,fn1,$2,FILENAME,f1[$1]
    }
  }
  else{
    print $1,"is only present in filename:", FILENAME
  }
}
END{
  for(id in f1){
    if( !(id in f2) ){print id,"is only present in afilename:",fn1}
  }
}' file1 file2
d1128 is only present in filename: file2
w1453 is different; file1 value: 515 & file2 value: 500
e3001 is different; file1 value: 55 & file2 value: 75

答案4

awk 'function printUniq(Id, fName){
         printf("%s is only present in filename: %s\n", Id, fName)
}

{ fileName[nxtinput+0]=FILENAME }
!nxtinput{ Ids[$1]=$2; next }

($1 in Ids){ if($2!=Ids[$1])
                 printf ("%s has difference, %s value: %s & %s value: %s\n",\
                 $1, fileName[0], Ids[$1], fileName[1], $2);
             delete Ids[$1];
             next
}
{ printUniq($1, fileName[1]) }
END{ for(id in Ids) printUniq(id, fileName[0]) }' file1 nxtinput=1 file2

相关内容