Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering text file to get unique entries based on value in column 3

I know a bit of bash, but I am facing a problem to filter a file. I'll explain with an example :

Given a text file like the following (file1):

10.10.12 bib24 Avenger goodone
10.10.12 bib21 The_Dark_Knight_Rises betterone
10.10.12 bib53 Avenger goodone
10.10.12 bib35 Ice_Age wow
11.10.12 bib53 TheAmazingSpiderMan nice
11.10.12 bib54 TheAmazingSpiderMan nice
11.10.12 bib01 Avenger goodone
12.10.12 bib29 Avenger goodone
12.10.12 bib11 TheAmazingSpiderMan nice
12.10.12 bib03 Ice_Age wow
12.10.12 bib98 Ice_Age wow
14.10.12 bib12 Ice_Age wow

This is the result I want (file2):

10.10.12 bib24 Avenger goodone
10.10.12 bib21 The_Dark_Knight_Rises betterone
10.10.12 bib35 Ice_Age wow
11.10.12 bib53 TheAmazingSpiderMan nice

So my question is : which command do I use to get this result (file2) ? (i.e. first entry of a movie, and not taking into account columns/fields 1, 2, and 4).

I hope it's clear enough.

like image 592
minutemaid Avatar asked Jan 16 '23 07:01

minutemaid


1 Answers

Here's one way using awk:

awk '!a[$3]++' file.txt

Results:

10.10.12 bib24 Avenger goodone
10.10.12 bib21 The_Dark_Knight_Rises betterone
10.10.12 bib35 Ice_Age wow
11.10.12 bib53 TheAmazingSpiderMan nice
like image 139
Steve Avatar answered Jan 31 '23 00:01

Steve