【问题标题】:Unix Delete Duplicate rows from csv based on 2 columnsUnix基于2列从csv中删除重复行
【发布时间】:2017-05-24 22:29:06
【问题描述】:

我有一个包含多列的 csv 文件。有些可能在第 4 列 (col4) 上有重复。

我需要删除出现重复的整行并只保留 1 行。该行的决定是通过从 col1 中获取最高值来做出的。

下面是一个例子:

col1,col2,col3,col4 

1,x,a,123

2,y,b,123

3,y,b,123

1,z ,c,999

在第 1 行和第 2 行和第 3 行中发现重复,只应保留第三行,因为 col1(row3) > col1(row2) > col1(row1)。

现在这段代码删除 col4 中的重复项而不查看 col1

awk '!seen[$4]++' myfile.csv

我想添加一个条件来检查 col1 的每个重复项并删除 col1 中值最低的那些并保留最高值 n col1 的行

输出应该是:

col1,col2,col3,col4

3,y,b,123

1,z,c,999

谢谢!

【问题讨论】:

  • 不,这不清楚,请您将更多信息和示例 Input_file 和预期输出放入帖子中,以便大家在这里提供帮助。
  • 有输入输出示例请仔细阅读。

标签: csv unix duplicates


【解决方案1】:

@Mr Smith:您能否尝试关注一下,如果这对您有帮助,请告诉我。

awk -F"[[:space:]]+,[[:space:]]+"  'FNR==NR{A[$NF]=$1>A[$NF]?$1:A[$NF];next} (($NF) in A) && $1 == A[$NF] && A[$NF]{print}'   Input_file  Input_file

编辑:尝试:

awk -F","  'FNR==NR{A[$NF]=$1>A[$NF]?$1:A[$NF];next} (($NF) in A) && $1 == A[$NF] && A[$NF]{print}' Input_file   Input_file

EDIT2: Following is explanation as per OP's request:
awk -F","                               ##### starting awk here and mentioning field delimiter as comma(,).
'FNR==NR{                               ##### FNR==NR condition will be TRUE only when Input_file first time is getting read.
                                              Because we want to save the values of last field as an index in array A and whose value is $1.
                                              So FNR and NR are the awk's default keywords, where the only difference between NR and FNR is 
                                              both will tell the number of lines but FNR will be RESET each time a new Input_file is being read,
                                              where NR will be keep on increasing till all the Input_files are completed. So this condition will be 
                                              TRUE only when first Input_file is being read.
A[$NF]=                                 ##### Now making an array named A whose index is $NF(last field of that array), then I am checking a condition
$1>A[$NF]                               ##### Condition here is if current line's $1 is greater than the value of A[$NF]'s value(Off course $NF last fields
                                              will be same for them then only they will be compared, so if $1's value is greater than A[$NF]'s value then 
?                                       ##### Using ? wild character means if condition is TRUE then perform following statements.
$1                                      ##### which is to make the value of A[$NF] to $1(because as per your requirement we need the HIGHEST value)
:                                       ##### If condition is FALSE which I explained 2 lines before than : operator indicates to perform actions which are following it.
A[$NF];                                 ##### Keep the value of A[$NF] same as [$NF] no change in it.
next}                                   ##### next is an awk's in built keyword so it will skip all further statements and take the control to again start from
                                              very first statement, off course it is used to avoid the execution of statements while first time Input_file is being read.
(($NF) in A) && $1 == A[$NF] && A[$NF]{ ##### So these conditions will be executed only and only when 2nd time Input_file is being read. Checking here 
                                              if $NF(last field of current line) comes in array A and array A's value is equal to first field and array A's value is NOT NULL.
print                                   ##### If above all conditions are TRUE then print the current line of Input_file
}' Input_file   Input_file              ##### Mentioning the Input_files here.

【讨论】:

  • 我试过了,结果是一样的,没有变化,重复的仍然存在。
  • 当然他们会在那里,当你发布时我猜你没有使用代码标签等,所以当时在字段之间有空间所以我已经给出了相应的解决方案,请你试试我编辑的解决方案?
  • 为什么代码中有 2 次 Input_file Input_file ?你能解释一下吗
  • 一个最重要的原因是为了保持输出行的顺序与 Input_file 的行相同,虽然我已经编辑了我的解决方案并附有解释,如果您对此有任何疑问,请告诉我。跨度>
猜你喜欢
  • 2016-12-02
  • 2016-04-26
  • 1970-01-01
  • 2015-12-17
  • 1970-01-01
  • 2022-01-24
  • 1970-01-01
  • 2015-12-28
相关资源
最近更新 更多