Alex Doc
Alex Doc

Reputation: 297

Removing duplicates interpreted according to a pattern, fails

I have some problems with these awk-sed scripts

sed -E 's/(^[a-zA-Z]+)([[:space:]]+[a-zA-Z]+){0,1}.*/\1\2/' file1.txt | sort | uniq > zz.txt

or other equivalent (windows GNU)

awk "match($0, /^[a-zA-Z]+( +[a-zA-Z]+)?/) {unq[substr($0, 1, RLENGTH)]} END {for (i in unq) print i}" file.txt > ZZ.txt

or another for linux is

sed -E 's/\s+\S+\s+[0-9]\S+$|[0-9].*//;s/(\S+\s+\S+)\s+\S+$/\1/
        H;x;s/((\n[^\n]*)(\n[^\n]*)*)\2$/\1/;x;$!d;x;s/.//' file

What do these scripts do? Get a list like this

Chi Allegati N.22021.htm
Casabella Marzo 2021.pdf
Casabella Febbraio 2021.pdf
Chi Allegati N.22021.pdf

after you have used the sed / awk scripts you will get this output

Casabella
Chi Allegati

Where then is the problem? Okay, now consider another list

(Ebook ITA Fumetti) Genius 021 Bersaglio A Gog.cbr
(Ebook ITA Fumetti) Genius 022 Poker Di Delitti.cbr

Corna Vissute - La moglie del ladro.cbz
Corna vissute - Nuova serie 011 - Un'offerta irresistibile.cbr

Il piccolo sceriffo 20 (Dardo 1992-02) [c2c CapitanUltra].cbz
Il piccolo sceriffo 23 (Dardo 1992-05) [c2c dinofelix].cbr

Il mondo di Arkadi - Caza 01 - Gli occhi di Or'Fe (by aquila & Janesek).cbr
Il mondo di Arkadi - Caza 02 - Il grande esterno (by aquila & Janesek).cbr

Il segno di Zorro 21 (Edizioni del fanciullo 9-1976)(BYPico57).cbr
Il segno di Zorro Serie d'oro 07 - Il mistero della miniera (Edizioni del fanciullo 1975-10-01) [c2c Dinofelix].cbr

L'Uomo Mascherato 06 - Il ratto di diana (Nerbini 1938-09) [c2c Scaricatore17-Gitra].cbr
L'Uomo Mascherato 08 - Il ritorno di Diana (Nerbini 1940-10) [c2c Scaricatore17-Gitra].cbr

Le leggende di Batman 017 (Play Press 1997-12 c2c) By Samox.cbz
Le leggende di Batman 018 (Planeta 1998-01 c2c) [Play Press] By Samox.cbz

Unfortunately the script no longer works well. In fact, I would like to obtain this output (or something very close), but it is not possible

    Corna Vissute
    Genius
    Il mondo di Arkadi
    Il piccolo sceriffo
    Il segno di Zorro
    Il segno di Zorro Serie d'oro
    L'Uomo Mascherato
    Le leggende di Batman

Logic behind script:

  1. Manipulate the line to contain only the fields required.
  2. Append to the hold space.
  3. Swap to the hold space, remove duplicate lines and swap back to the pattern space.
  4. Delete all but the last line.
  5. On the last line, swap to the hold space, remove the first newline and print the results.

EDIT: Add another example that script process incorrectly

I have this list

A porte chiuse 054 - Come la prima notte (Ediperiodici 1985-11) [noc2c Charles].cbr
A porte chiuse 097 - Il demone nella bottiglia (Ediperiodici 1989-06) [c2c noedit Charless].cbr
A Porte Chiuse 138-139 - Una viziosa incallita.cbr
A porte chiuse Special 13 - Il primo amore-Il viziaccio [228pag] (Ediperiodici 1986-03) [c2c Charles-Edit Gitra].cbr
A Porte Chiuse Special 020 [Fumetti.Erotici](1400~).cbz

but I will expect only this output

A porte chiuse
A porte chiuse special

But script return this

A Porte
A porte

EDIT: Another problem: I have

Il destino di Kakugo - vol 09 (MANGA ITA)(Scan & Edit by DDT)(DDT0580)(MQ jpg).cbr
Il mensile di Barbapap… N. 02 - [A.Mondadori] [1977-01] [ITA][c2c](no pagina centrale).cbz

but it return me

il
Il destino di Kakugo
Il mensile di Barbapap

or I have

Y L'Ultimo Uomo.-.Ragazze vol 07.-.(Magic Press)(c2c aquila).cbr
Y L'ultimo Uomo [08] Bambole Di Carta (Magic Press)(c2C Aquila).cbr
Yiu 01 - Inferno.cbr
Yiu 03 - Assassini (Scanlation, Ita, By Eleinad For Lc22Db).cbr

but it return me

Y
Y L'ultimo Uomo
Yiu

But I expect

Il destino di Kakugo
Il mensile di Barbapap
Y L'ultimo Uomo
Yiu

Upvotes: 0

Views: 148

Answers (2)

Thomas Hansen
Thomas Hansen

Reputation: 777

Here is a sed alternative:

sed -E 's/(^[^0-9\-\.\[\…\-]* ?).*/\1/' input | sort -u

Upvotes: 0

anubhava
anubhava

Reputation: 785316

This modified awk script from my last answer should work for you:

awk '{sub(/^\([^)]+\) */, "")} match($0, /^[a-zA-Z][a-zA-Z\047]*( +[a-zA-Z\047]+)*/) {s = substr($0, 1, RLENGTH); unq[tolower(s)]=s} END {for (i in unq) print unq[i]}' file.txt

Il segno di Zorro Serie d'oro
L'Uomo Mascherato
Il mondo di Arkadi
Il piccolo sceriffo
Le leggende di Batman
Corna vissute
Il segno di Zorro
Genius

A more readable version:

awk '
{sub(/^\([^)]+\) */, "")}
match($0, /^[a-zA-Z][a-zA-Z\047]*( +[a-zA-Z\047]+)*/) {
   s = substr($0, 1, RLENGTH)
   unq[tolower(s)] = s
}
END {
   for (i in unq)
      print unq[i]
}' file.txt

Upvotes: 2

Related Questions