Filtrar lineas de texto repetidas

Por Saiyine  el 2003-08-05 00:00:00 - Secciones:  PERL  - Enlace permanente: 393

Necesitaba un programa que eliminase las lineas repetidas de un texto, y como el uniq del UNIX es practicamente inutil, tuve que currarmelo yo (unique):

#!/usr/bin/perl @lineas = <STDIN>; foreach $linea (@lineas) { $estaba="Pono"; foreach $cadena (@repasadas) { if ($cadena eq $linea) { $estaba="Pozi"; } } if ($estaba eq "Pono") { push @repasadas, $linea; print "$linea"; } }

ACTUALIZACIÓN 23/07/2004 Usando diccionarios, _muchísimo_ más rápido.

#!/usr/bin/perl while (<STDIN>) { if (!(exists $lineas{$_})) { print "$_"; $lineas{$_}="1"; } }

Keith Amling (25/09/2005, 13:22)

uniq is designed for use with sorted text. If you sort the input first it will handle it correctly, for example
$ cat file
c
a
a
b
a
$ cat file | uniq
c
a
b
a
$ cat file | sort | uniq
a
b
c

If you need the lines sorted as they were initially then uniq is useless. While I'm on the subject of uniq, don't forget uniq -c.

Saiyine (25/09/2005, 23:41)

Yeah, that was right the problem, I didn't wanted sort to mess with the order. Thanks for this lot of commentaries!


Tu nombre (Nick):

Tu correo (Email):
  Necesario para tu gravatar!

Tu página (URL):

Escribe aqui tu comentario:

       :noworry: :roll: :huh: :push: ¬¬ :OO 8) 8O :( :) :? :D :P :o :x :| ;) ^^ xD



Vista previa activada.

Vista previa (6/6/6, 6:66)


Patrocinadores

Saiyine recommends the easiest way to earn money with your web: get paid just by having some links! Click this button to check it out.

Text Link Ads

. . .

Descargas

  • ApagaPC
    apagapc241.exe  (2111)
  • LimpiaDocus
    LimpiaDocus001.exe  (1827)
  • RCM
    rcm001.zip  (1689)
  • Popmail
    popmail-0.4-psmn.tar.gz  (1736)
  • Manual de Delphi en PDF
    delphi_pdf.zip  (2238)
  • Evangelio del Perl
    Evangelio_del_perl.pdf  (1864)
  • Excel Simpsons
    Excel Simpsons  (1914)
  • . . .

    Recomiendo

  • Drivers Nvidia 
    96.85 (676)
  •  Mozilla Firefox 
    DIR (610)
  •  Mozilla Thunderbird 
    DIR (576)
  • KLite 
    klmcodec375.exe (115)
  •  Emule 
    DIR (532)
  •  Knoppix 
    DIR (453)
  •  Knoppix en español (DVD) 
    DIR (795)
  •  Mldonkey 
    DIR (511)
  •  Nucleo linux 
    DIR (516)
  •  Parche linux 
    DIR (516)

    . . .
  • Proyectos online

  • Saiyine Store
  • Kunowalls!!!
  • Fondos de pantalla
  • Picaday: imagenes sexys o chocantes.
  • Scarlett: fotos de Scarlett Johansson
  • WhatsmyIP: obtener tu IP pública
  • Uma Thurman: galeria de Uma Thurman
  • FunPics: imagenes graciosas
  • . . .

    Blogs

  • Por lo que más querais, no entreis a estos: Tapanez, Yhandros, Onez.

  • Otros en español: Chavalina, Testosterona, Kirai, Wardog y el mundo, Mundo Geek, Microsiervos.

  • Mis lecturas en guiri: Coding Horror, MySQL Performance, Slashdot.

    . . .
  • Utilidades

  •  Coral  (624), cacheando webs
  •  Spam.la  (723), correo de usar y tirar
  •  Mailinator  (712), correo de usar y tirar
  •  Bug me not  (656), absurdos registros di NO
  •  Trashmail  (646), correo de usar y tirar

    . . .
  • Saipuntos

    Jomer: 2
    Yhandros: 1
    Cheimy: 1

    . . .

    Faq

  • ¿MlDonkey para windows?
    Pues si, ya hay un port, y lo podeis encontrar aqui.
  • ¿Imagenes del universo?
    Puesss, mi favorita es astrored, aunque debe haber muchas similares.
  • ¿Drivers para voodoo?
    Probad aqui para la voodoo 3.
  • . . .

    Página web ©2001-2008 Saiyine generada en 0.03782s, con 1735515 visitas en total, hoy 652 (de un total de 1640 previstas).

    . . .