Artificial intelligent assistant

Record of highest NF Using `awk` for the following input file, how could I take the record with highest `NF` for the same last field. File: 5541,55004,919843,IND 5542,5541,55004,919843,IND 7903,790287,RUSE 7905,7903,790287,RUSE Needed output: 5542,5541,55004,919843,IND 7905,7903,790287,RUSE

You can leverage two arrays, one to keep the number of fields count, and one to keep the whole record, and the loop over the second array at the `END`:


awk -F, 'NF>a[$NF] {a[$NF]=NF; b[$NF]=$0} END{for (i in b) print b[i]}' file.txt


**Example:**


% cat file.txt
5541,55004,919843,IND
5542,5541,55004,919843,IND
7903,790287,RUSE
7905,7903,790287,RUSE

% awk -F, 'NF>a[$NF] {a[$NF]=NF; b[$NF]=$0} END{for (i in b) print b[i]}' file.txt
5542,5541,55004,919843,IND
7905,7903,790287,RUSE

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy ab82a446bd9b501ed763cc5372a1ac78