Re: Looking for regex



On Jan 26, 2008 1:13 PM, John Kufrovich <jkufrovich@xxxxxxxxxxxxx> wrote:
Chas,
Thanks for the help. I'm not expert but I think your code will output each
line.
snip

You don't have to be an expert to run the code and see what it does
(it only prints out the hour and temp for the first instance of each
hour). If you want to print out the whole line for the first instance
of each hour you can simplify the regex and the print:

#! /usr/bin/perl

use strict;
use warnings;

my %seen;
while (<DATA>) {
my ($hour) = / (..)/;
print unless $seen{$hour}++;
}

__DATA__
2007-01-23 00:01:00,43.3,34.9,30.14,North,359,7,8,72,0.00,,,0.00,Wunderground
v.1.13,
2007-01-23 00:06:00,43.2,34.8,30.14,North,354,11,11,72,0.00,,,0.00,Wunderground
v.1.13,
2007-01-23 00:12:00,43.2,34.8,30.14,North,1,6,9,72,0.00,,,0.00,Wunderground
v.1.13,
2007-01-23 01:11:00,42.9,34.8,30.13,North,1,5,6,73,0.00,,,0.00,Wunderground
v.1.13,
2007-01-23 01:17:00,42.9,34.8,30.12,NNW,346,3,7,73,0.00,,,0.00,Wunderground
v.1.13,
2007-01-23 01:23:00,43.1,35.0,30.12,North,359,5,11,73,0.00,,,0.00,Wunderground
v.1.13,
2007-01-23 02:14:00,43.2,34.1,30.10,North,0,16,16,70,0.00,,,0.00,Wunderground
v.1.13,
2007-01-23 02:19:00,43.2,34.1,30.09,North,0,6,13,70,0.00,,,0.00,Wunderground
v.1.13,
2007-01-23 02:24:00,43.2,33.7,30.09,North,349,8,14,69,0.00,,,0.00,Wunderground
v.1.13,
2007-01-23 03:04:00,43.2,34.1,30.11,North,0,7,12,70,0.00,,,0.00,Wunderground
v.1.13,
2007-01-23 03:09:00,43.2,34.1,30.11,North,0,10,17,70,0.00,,,0.00,Wunderground
v.1.13,

This can be further refined into a single command:

perl -ne '($h)=/ (..)/;print unless $s{$h}++' file_to_filter
.



Relevant Pages

  • Re: Dealing with hijackers
    ... In some circumstances that would provide a good explanation. ... All homepage sites are named using the user name of the subscriber. ... first instance appeared to match that interpretation. ...
    (alt.support.diabetes)
  • Re: Coding Practices, Best methods?
    ... happening but i think its checking the first instance and then it goes ... Set objQRY = objWMIService.ExecQuery ("Select * from ...
    (microsoft.public.scripting.vbscript)
  • Re: Barclaycard: changing from 0870 to 0844
    ... :: This may be the first instance of Ofcom's wish for 0870 ... saying the same thing. ... No sign of a geo number..... ...
    (uk.telecom)
  • Re: The Naked Rambler - initial impressions
    ... It is to grab people's attention - which on ... was it not Winston Churchill who offered to pay the suffragettes ... In the first instance please reply to group, ...
    (uk.rec.naturist)
  • Re: Barclaycard: changing from 0870 to 0844
    ... :: This may be the first instance of Ofcom's wish for 0870 ... No sign of a geo number..... ... Ivor ...
    (uk.telecom)