FILTRAR LINEAS DE TEXTO REPETIDAS - .: .: Saiyine :. :.

Filtrar lineas de texto repetidas

Por Saiyine Enviar correo 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!

Otras entradas de la web que podrian interesarte:

o o o o 


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)


Fondos de pantalla

. . .

Recomendamos


. . .

Descargas

  • ApagaPC
    apagapc241.exe  (4339)
  • LimpiaDocus
    LimpiaDocus001.exe  (3627)
  • RCM
    rcm001.zip  (3062)
  • Popmail
    popmail-0.4-psmn.tar.gz  (3132)
  • Manual de Delphi en PDF
    delphi_pdf.zip  (3764)
  • Evangelio del Perl
    Evangelio_del_perl.pdf  (3786)
  • Excel Simpsons
    Excel Simpsons  (3789)
  • . . .

    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: El Mundo Today, La libreta de Van Gaal, Chavalina, Kirai, Mundo Geek, Microsiervos.

  • Mis lecturas en guiri: Michael Yon, Coding Horror, YCombinator news, MySQL Performance, Slashdot.

    . . .
  • Utilidades

  •  Coral  (1583), cacheando webs
  •  Spam.la  (1899), correo de usar y tirar
  •  Mailinator  (1802), correo de usar y tirar
  •  Bug me not  (1580), absurdos registros di NO
  •  Trashmail  (1629), correo de usar y tirar

    . . .

  • Página web ©2001-2011 Saiyine descargada en s, con 5908053 visitas en total, hoy (de un total de 0 previstas).

    . . .