2008-01-02: first entry
2008-02-03: second entry on two lines
here is the additional line
2008-03-04: third entry
has
three
extra lines
2008-04-05: fourth entry has just one on line again
Question :
Comment faire pour avoir le groupe de lignes qui contient un mot, par exemple « three » ?
Solution :
my @stuff;
while (<IN>) {
if (/^\s/) { $stuff[-1] .= $_; }
else { push @stuff, $_; }
}
print grep { /three/ } @stuff;
Avec la liste ip-to-country et un script Perl du style : #!/usr/bin/perl -w
use strict;
my %ip;
my $old_ip=0;
while (<>) {
my ($ip) = split;
next if ($ip eq $old_ip);
my ($a,$b,$c,$d) = split(/\./,$ip);
my $n = ((((($a * 256) + $b) * 256) + $c) * 256) +$d;
open (FILE,"< ip-to-country.csv") || die $!;
while (<FILE>) {
my ($beg,$end,$country) = (split (/,/,$_))[0,1,4];
$beg =~ s/"//g;
$end =~ s/"//g;
if (($beg <= $n) and ($end >= $n)) {
$ip{$ip}++;
print "ip = $ip\n";
last;
}}
close(FILE);
$old_ip=$ip;
}
exit(0);
Avec :
# mktime(sec, min, hour, mday, mon, year, wday = 0, yday = 0, isdst = 0)
#
# The month (mon), weekday (wday), and yearday (yday) begin at zero.
# The year (year) is given in years since 1900.
# Sunday is 0, not 1;
# January 1st is 0, not 1.
#
# I.e. January is 0, not 1;
# I.e. The year 1995 is 95; the year 2001 is 101.
# I.e. Calendar time for December 12, 1995, at 10:30 am : mktime( 0, 30, 10, 12, 11, 95 );
#
# Greenwich Mean Time (GMT)
# Daylight saving Time (DST)
# Coordinated Universal Time (CUT)
##################################
Après avoir acheté pour une trentaine d’euros un ensemble carte 8 relais + controleur USB (cf.photo), me voici parti dans la prog. du port série.
1. Module pour faire de l’USB un port série
usbserial + ftdi_sio (/dev/ttyUSB0)
2. Le Perl script : #!/usr/bin/perl -w
use Device::SerialPort;
use strict ;
my $str = « » ;
my @cmd = () ;
# Set up the serial port
# 9600, 81N on the USB ftdi driver
my $port = Device::SerialPort->new(« /dev/ttyUSB0″);
$port->databits(8);
$port->baudrate(9600);
$port->parity(« none »);
$port->stopbits(1);
$port->handshake(« none »);
$port->write_settings || die(« Could not set up port »);
Follow Me!