Bash 如果CSV 2中的字段与CSV 1中的字段匹配,则删除第二个CSV的行

Bash 如果CSV 2中的字段与CSV 1中的字段匹配,则删除第二个CSV的行,bash,csv,terminal,Bash,Csv,Terminal,我有两个CSV,看起来像下面这样 CSV 1 CSV 2 我只想要输出文件中ID为003的行。因为有300-500万行,所以它必须是终端中的某些内容,而不是excel 对不起,这些桌子看起来很糟糕 提前感谢。请尝试以下方法: awk 'NR==FNR {if (FNR>1) seen[$1]++; next} !seen[$1] {print}' csv1.txt csv2.txt 断行解释: awk ' NR==FNR { # t

我有两个CSV,看起来像下面这样

CSV 1 CSV 2 我只想要输出文件中ID为003的行。因为有300-500万行,所以它必须是终端中的某些内容,而不是excel

对不起,这些桌子看起来很糟糕


提前感谢。

请尝试以下方法:

awk 'NR==FNR {if (FNR>1) seen[$1]++; next} !seen[$1] {print}' csv1.txt csv2.txt
断行解释:

awk '
    NR==FNR {
                        # this block is evaluated while reading csv1.txt only
        if (FNR > 1)    # skip the header line of csv1.txt
            seen[$1]++  # memorize the occurance of the ID
        next            # prevents from continuing to the next block
    }
                        # the lines below is evaluated while reading csv2.txt only
    ! seen[$1] {        # if the ID is not in the array (not included in csv1.txt)
        print           # then print the row
    }
' csv1.txt csv2.txt
  • 只有在读取第一个文件时才满足条件
    NR==FNR
    在参数列表中(
    csv1.txt,在本例中为
    )。
    这是为每个文件执行不同代码的常用习惯用法
  • 语句
    seen[$1]+
    在输入行的第一个字段上设置一个标记。 在处理过程中,您很容易知道ID是否包含在
    csv1.txt
    csv2.txt

请尝试以下方法:

awk 'NR==FNR {if (FNR>1) seen[$1]++; next} !seen[$1] {print}' csv1.txt csv2.txt
断行解释:

awk '
    NR==FNR {
                        # this block is evaluated while reading csv1.txt only
        if (FNR > 1)    # skip the header line of csv1.txt
            seen[$1]++  # memorize the occurance of the ID
        next            # prevents from continuing to the next block
    }
                        # the lines below is evaluated while reading csv2.txt only
    ! seen[$1] {        # if the ID is not in the array (not included in csv1.txt)
        print           # then print the row
    }
' csv1.txt csv2.txt
  • 只有在读取第一个文件时才满足条件
    NR==FNR
    在参数列表中(
    csv1.txt,在本例中为
    )。
    这是为每个文件执行不同代码的常用习惯用法
  • 语句
    seen[$1]+
    在输入行的第一个字段上设置一个标记。 在处理过程中,您很容易知道ID是否包含在
    csv1.txt
    csv2.txt

请您添加您为解决您自己的问题所付出的努力,然后告知我们。此外,请将您的样本/代码包装在代码标签中,以使其清晰。我不知道如何解决此问题。只是询问是否有办法做到这一点。请您添加您为解决您自己的问题所付出的努力,然后让我们知道。此外,请将您的示例/代码包装在代码标签中以使其清晰。我不知道如何解决此问题。只是问问是否有办法做到这一点。