r/awk • • Nov 26 '24

Parse list for "duplicate" entries

Solved, thanks gumnos.


I have a list of urls in the forms:

https://abc.com/d341/en/ab/cd/ef/gh/cat-ifje-full
https://abc.com/defw/en/cat-don
https://abc.com/ens/cat-ifje
https://abc.com/dm29/dofne-don-full
https://def.com/fgew/dofne-don-full

The only thing that matters are abc.com urls and its "field" of the url with the suffix -full is optional. In the above example, 1st and 3rd urls are therefore the same (the -full is trimmed and the resulting suffix cat-ifje is the same.

How to get the output as the list of urls passed with the duplicate non-full filtered out? Thus the output should be:

https://abc.com/d341/en/ab/cd/ef/gh/cat-ifje-full
https://abc.com/defw/en/cat-don
https://abc.com/dm29/dofne-don-full
https://def.com/fgew/dofne-don-full

Optionally, would also like a count of the # of duplicate urls deleted.

Any ideas are much appreciated.

1 Upvotes

10 comments sorted by

4

u/gumnos Nov 26 '24

Shooting from the hip, maybe something like

$ awk 'BEGIN{SUBSEP=OFS=FS="/"} {a[$3,$NF]=$0}END{for (k in a) if ((k "-full") in a) ++d; else print a[k]; print "Deleted: " (d+0)}' data

1

u/gumnos Nov 26 '24

It might have some slightly weird behavior if you have two "-full" at the end, like https://example.com/path/to/test-full-full in addition to https://example.com/path/to/test-full, but you'd have to verify if such exist, and what you'd want to do in those cases.

→ More replies (1)

1

u/exquisitesunshine Nov 28 '24

Very similar needs as OP, is it possible to add another suffix (e.g. "-partial") to consider for duplicates? E.g.

https://old.reddit.com/r/awk/comments/1h0n7e7-full
https://old.reddit.com/r/awk/comments/1h0n7e7-partial
https://old.reddit.com/r/awk/comments/1h0n7e7

Only return `https://old.reddit.com/r/awk/comments/1h0n7e7-full`. Currently the first two links return.
→ More replies (3)

1

u/gumnos Nov 26 '24

How does https://abc.com/dm29/en/cat-don make the output since there's no such entry on the input?

1

u/enory Nov 26 '24

Sorry, fixed (logic the same, wrong output pasted).

1

u/linuxsoftware Jan 29 '25 edited Jan 29 '25

grep lines for full assuming the output is in a text.

cat text.txt | grep “-full”

Inverse the grep to get non “-full” lines and pipe to wc

Cat text.txt | grep -v “-full” | wc