2017-01-10 2 views
1

Ich habe eine CSV-Datei mit mehreren Spalten. Einige haben möglicherweise Duplikate über den 4. col (col4).Unix Löschen Doppelte Zeilen von CSV basierend auf 2 Spalten

Ich muss die ganze Zeile, wo die Duplikate auftritt, löschen und nur 1 Zeile behalten. Die Entscheidung dieser Zeile wird getroffen, indem der höchste Wert von Spalte 1 erhalten wird.

Unten ist ein Beispiel:

col1,col2,col3,col4 

1,x,a,123 

2,y,b,123 

3,y,b,123 

1,z ,c,999 

Duplizieren in Zeile 1 und row2 und row3 gefunden werden, sollte nur dritte Reihe gehalten werden, weil spalte1 (row3)> spalte1 (row2)> spalte1 (row1) .

Für diesen Code nun Duplikate in col4 löschen, ohne auf col1 suchen

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

Ich möchte eine Bedingung hinzuzufügen col1 für jede Duplikate zu überprüfen und diejenigen mit dem niedrigsten Wert in col1 löschen und die Zeile zu halten mit höchster Wert n col1

Ausgang sollte sein:

col1, col2, col3, col4

3,y,b,123 

1,z,c,999 

Vielen Dank!

+1

Nein, das nicht klar ist, könnten Sie weitere Informationen setzen und input_file am erwarteten Ausgabe in Post, probieren Sie bitte so, dass alle hier helfen könnten. – RavinderSingh13

+0

Es gibt ein Eingabe- und Ausgabebeispiel, bitte lesen Sie es sorgfältig. –

Antwort

0

@Mr Smith: Könnten Sie bitte versuchen, folgen und lassen Sie mich wissen, wenn dies Ihnen hilft.

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: Versuchen:

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

ich tred es, Ergebnis war das gleiche keine Änderung Duplikate sind immer noch da. –

+0

Natürlich werden sie da sein, wenn du gepostet hast, dann denke ich, dass du keine Code-Tags oder ähnliches benutzt hast, so dass zwischen diesen Feldern Platz war, also habe ich eine entsprechende Lösung gegeben. Könntest du bitte meine bearbeitete Lösung ausprobieren? – RavinderSingh13

+0

warum 2 mal Input_file Input_file im code? Könnten Sie bitte erklären, –

Verwandte Themen