Categories
Linux Mastering Development

Uniq based on last field, keeping last line, and append number of duplicates

I need some help to implement awk command in a bash script for implementing below logic. Tried sort/uniq combination, but no luck.

I have a long list with time stamp and some number in front of it like below.

[2020-09-15 09:03:21.835335] 1021
[2020-09-15 09:03:21.935335] 1021
[2020-09-15 09:03:22.835335] 1022
[2020-09-15 09:03:25.835335] 1022
[2020-09-15 09:04:21.835335] 1023
[2020-09-15 09:05:21.835335] 1023
[2020-09-15 09:04:22.835335] 1023
[2020-09-15 09:05:23.835335] 1023

In the final outcome, I want to process the list with respect to column 2 above with numbers. In the final processed data, I wanted to have count of unique numbers (even if the occurrence is just once) from column 2 and add it in a new column in front of number. What I really needed was to have time stamp from last occurrence of duplicate/unique entries from number column.

[2020-09-15 09:03:21.935335] 1021 2
[2020-09-15 09:03:25.835335] 1022 2
[2020-09-15 09:05:23.835335] 1023 4

Any help will be appreciated!

Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *