AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 698668
Accepted
Sollosa
Sollosa
Asked: 2022-04-11 06:32:38 +0800 CST2022-04-11 06:32:38 +0800 CST 2022-04-11 06:32:38 +0800 CST

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

  • 772

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

# cat file1
e3001 75
n5244 30
w1453 500

#cat file2
d1128 30
w1453 515
n5244 30
e3001 55

要考虑的事情。

  1. userIds 可能不会在两个文件中完全排序
  2. userId 的数量可能因文件而异

必需的

  • 首先,file1:column1 中的 userId 必须与 file2:column1 中的 UserId 匹配
  • 接下来将它们在 file1:column2 中的值与 file2:column2 进行比较
  • 打印值有差异的地方。如果有的话,还有额外的用户 ID

输出:

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
bash shell-script
  • 5 5 个回答
  • 315 Views

5 个回答

  • Voted
  1. Best Answer
    RudiC
    2022-04-11T06:55:33+08:002022-04-11T06:55:33+08:00

    嗯——可以这么说,你的剧本走的是风景优美的路线。一个简单的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数组索引,如果存在,则打印差异(如果没有则不打印),并deletes 数组元素(delete可能是在某些 awk 实现中缺少,顺便说一句)。如果不存在,请相应打印。在该END部分中,将打印所有剩余的数组元素,因为它们仅存在于 file1 中。

    • 3
  2. terdon
    2022-04-11T07:45:53+08:002022-04-11T07:45:53+08:00

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

    话虽如此,这是另一种 awk 方法(坦率地说,RudiC更好,但我已经写了这个,所以无论如何我都会发布):

    $ 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
    
    • 2
  3. DanieleGrassini
    2022-04-11T08:31:53+08:002022-04-11T08:31:53+08:00

    评论不言自明:

    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
    
    • 2
  4. αғsнιη
    2022-04-11T08:22:55+08:002022-04-11T08:22:55+08:00
    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
    
    • 1
  5. Ed Morton
    2022-04-11T11:26:21+08:002022-04-11T11:26:21+08:00

    基本上与 RudiC 发布的解决方案相同,但没有全部大写的变量名称,并且对清晰度进行了其他一些小的改进:

    $ cat tst.awk
    NR==FNR {
        file1[$1] = $2
        next
    }
    $1 in file1 {
        if ( $2 != file1[$1] ) {
            printf "%s has difference, %s value: %s & value: %s\n", $1, ARGV[1], file1[$1], FILENAME, $2
        }
        delete file1[$1]
        next
    }
    {
        print $1, "is only present in filename:", FILENAME
    }
    END {
        for ( id in file1 ) {
            print id, "is only present in filename:", ARGV[1]
        }
    }
    

    $ awk -f tst.awk file1 file2
    d1128 is only present in filename: file2
    w1453 has difference, file1 value: 500 & value: file2
    e3001 has difference, file1 value: 75 & value: file2
    
    • 1

相关问题

  • 从文本文件传递变量的奇怪问题

  • 虽然行读取保持转义空间?

  • MySQL Select with function IN () with bash array

  • `tee` 和 `bash` 进程替换顺序

  • 运行一个非常慢的脚本直到它成功

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve