2017-01-10 14 views
1

私は複数の列を持つcsvファイルを持っています。いくつかは4列目(col4)に重複している可能性があります。Unix削除2列に基づいてcsvから重複行

重複が発生する行全体を削除し、1行だけを保持する必要があります。この列の決定は、col1から最大値を得ることによって行われます。以下

例である:重複を行1とROW2及びROW3に見出される

col1,col2,col3,col4 

1,x,a,123 

2,y,b,123 

3,y,b,123 

1,z ,c,999 

、唯一の第3段目は、理由COL1(ROW3)> COL1(ROW2)> COL1(ROW1)に維持されるべきです。今、このコードは、col1のを見ずにCOL4で重複を削除するために

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

私は、各重複のCOL1をチェックし、COL1で最も低い値を持つものを削除して行を保つために条件を追加したいと思います最高値のn

col1の出力は次のようになります。

COL1、COL2、COL3、COL4

3,y,b,123 

1,z,c,999 

ありがとうございました!

+1

いいえ、これは明らかではない、すべてがここに助けることができるようにあなたは、ポストにINPUT_FILE ANS期待される出力をより多くの情報を入れて、サンプリングしてくださいでした。 – RavinderSingh13

+0

入出力の例がありますので注意して読んでください。 –

答えて

0

@ 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 

EDIT:試してみてください:

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. 
+0

私はそれを実行しましたが、結果は同じでした重複はまだそこにありません。 –

+0

もちろん、あなたが投稿したときにコードタグなどを使用していないと思いますので、その間にスペースが入っていますので、解決策を与えました。私の編集したソリューションを試してみてください。 – RavinderSingh13

+0

なぜ2回Input_file Input_fileコード内に?あなたは説明してください –

関連する問題