Bash 如果CSV 2中的字段与CSV 1中的字段匹配,则删除第二个CSV的行
我有两个CSV,看起来像下面这样 CSV 1 CSV 2 我只想要输出文件中ID为003的行。因为有300-500万行,所以它必须是终端中的某些内容,而不是excel 对不起,这些桌子看起来很糟糕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
提前感谢。请尝试以下方法:
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,在本例中为
)。 这是为每个文件执行不同代码的常用习惯用法
- 语句
在输入行的第一个字段上设置一个标记。 在处理过程中,您很容易知道ID是否包含在seen[$1]+
中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,在本例中为
)。 这是为每个文件执行不同代码的常用习惯用法
- 语句
在输入行的第一个字段上设置一个标记。 在处理过程中,您很容易知道ID是否包含在seen[$1]+
中csv1.txt
csv2.txt