Archive for febbraio, 2010
25
feb

Bene,

abbiamo un file di log da 600Mb con 8.791.937 milioni di righe… in formato access.log.gz fatto in questo modo:

1
2
3
[...]
IP.IP.IPP.IPP - - [14/Sep/2009:00:01:22 +0200] "GET /xml.php/getall?cmd=tile&format=jpeg&x=3777&y=5143&z=16&extra=2&ts=256&q=60&rdr=0&sito=test HTTP/1.1" 200 1713 "http://www.ilmiosito.it/call?v=373" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; GTB6)"
[...]

Cosa ci serve? L’url e i suoi parametri? Ok…

Da shell possiamo usare un ZCAT per leggere il file zippato senza aprirlo e magari fare un redirect con meno righe, giusto per fare dei test:

1
$ zcat /tmp/access.log.gz | grep "&x=" | head -2500 > /tmp/small.log

ora abbiamo un file più piccolo…

1
cut -d\" -f2 /tmp/small.log | cut -d\? -f2 | cut -d\  -f1 > CLEAR.log

ora abbiamo un file contenente solo l’url da parsare! Già meglio vero?


Adesso, con C# mettiamo su un parser senza perderci troppo tempo in shell e carichiamo il file!

Può essere comodo usare le liste, in modo da salvare la coppia:

(valore_che_serve),(quante_volte_si_ripete)

1
      var CityList = new Dictionary<string , int>();

e ovviamente salvare o incrementare i valori trovati:

1
2
3
4
       if ( ! CityList.ContainsKey( VALORE ))
              CityList.Add(VALORE, 1);
        else
              CityList[VALORE] = CityList[VALORE] + 1;

così abbiamo per ogni chiave il suo numero di ripetizioni!

Ma poi? Tutto disordinato?
No, no, mettiamo un pò di ordine, magari salvando un file CSV separato da “;” con i valori ordinati dal più ripetuto al memo ripetuto…

1
2
3
4
5
6
7
8
StreamWriter sw = new StreamWriter(@"Z:\alb\ordine.dat");

foreach (KeyValuePair</string><string , int> item in CityList.OrderByDescending(key => key.Value))
{
        sw.WriteLine(item.Key + ";" + item.Value);
        sw.Flush();
}
sw.close();

Ah, per usare le Dictionary e il metodo OrderBy, sono necessarie queste due librerie:

using System.Collections.Generic;
using System.Linq;


Bene, tutto qui!

Adesso abbiamo un file CSV ordinato, con il nostro valore preferito (IP, la città, un url…) e quante volte viene ripetuto!
Per ogni utilizzo!

enjoy.

[ref -> albertopasca.it]

FacebookTwitterDeliciousLinkedInGoogle BookmarksNetlogGoogle GmailMySpaceGoogle ReaderShare

, , , , , , , , , , ,

17
feb

Semplice semplice…

Perchè mai usare lo StreamReader per contare le linee in un file di testo???

1
2
3
4
5
6
7
8
StreamReader sr = new StreamReader(inputPath);
int lines = 0;
while (!sr.EndOfStream)
{
  sr.ReadLine();
  lines++;
}
sr.close();


…quanto si può facilmente fare un ReadAllLines?

1
     int lines = File.ReadAllLines(@"iltuofile.log").Length;

:)

[ref -> albertopasca.it]

FacebookTwitterDeliciousLinkedInGoogle BookmarksNetlogGoogle GmailMySpaceGoogle ReaderShare

, , , , , , , , , ,

16
feb

Per scrivere programmi in MIXAL è possibile utilizzare un qualsiasi editor di testo.

Sarebbe bello e piacevole colorare il codice no?

Ecco per voi la versione .uew per UltraEdit versioni recenti (almeno 15).
Il file va salvato in “AppData\Roaming\IDMComp\UltraEdit\wordfiles” del vostro disco.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/L20"MIXAL" MIXAL_LANG Line Comment = * String Chars = " File Extensions = mix
/Colors = 0,8421376,8421376,8421504,255,
/Colors Back = 16777215,16777215,16777215,16777215,16777215,
/Colors Auto Back = 1,1,1,1,1,
/Font Style = 0,0,0,0,0,
/C1 KEYWORDS Colors = 16711680 Colors Back = 16777215 Colors Auto Back = 1 Font Style = 0
NOP
ADD FADD
SUB FSUB
MUL FMUL
DIV FDIV
NUM CHAR HLT
SLA SRA SLAX SRAX
SLC SRC
MOVE
LDA LD1 LD2 LD3 LD4 LD5 LD6 LDX
LDAN LD1N LD2N LD3N LD4N LD5N LD6N LDXN
STA ST1 ST2 ST3 ST4 ST5 ST6 STX STJ STZ
JBUS IOC IN OUT JRED
JMP JSJ JOV JNOV JL JE JG JGE JNE JLE
JAN JAZ JAP JANN JANZ JANP
J1N J1Z J1P J1NN J1NZ J1NP
J2N J2Z J2P J2NN J2NZ J2NP
J3N J3Z J3P J3NN J3NZ J3NP
J4N J4Z J4P J4NN J4NZ J4NP
J5N J5Z J5P J5NN J5NZ J5NP
J6N J6Z J6P J6NN J6NZ J6NP
JXN JXZ JXP JXNN JXNZ JXNP
INCA DECA ENTA ENNA
INC1 DEC1 ENT1 ENN1
INC2 DEC2 ENT2 ENN2
INC3 DEC3 ENT3 ENN3
INC4 DEC4 ENT4 ENN4
INC5 DEC5 ENT5 ENN5
INC6 DEC6 ENT6 ENN6
INCX DECX ENTX ENNX
CMPA FCMP CMP1 CMP2 CMP3 CMP4 CMP5 CMP6 CMPX
EQU
CON
ALF
ORIG
END

Per chi usa Crimson Editor invece, può usare questo file di specifica (mixal.spc) e questo per le parole chiave (mixal.key).

Riferimenti: Programmazione degli Elaboratori – Andrea Tettamanzi (UNIMI)

[ref -> albertopasca.it]

FacebookTwitterDeliciousLinkedInGoogle BookmarksNetlogGoogle GmailMySpaceGoogle ReaderShare

, , , , , , , ,

02
feb

Rieccoci…
fresco fresco di mattinata!

E’ uno dei metodi che permette di rimuovere un elemento da un array semplice.
Esempio

1
string[] bigArray = new string[1234567];

Per array molto grandi può risolvere mooooolti problemi:

1
2
3
4
5
6
    private string[] removeAt(int idx)
    {
      var tmp = new List<string>(fileEntries);
      tmp.RemoveAt(idx);
      return tmp.ToArray();
    }


yep!

RIF: stackoverflow.com

[ref -> albertopasca.it]

FacebookTwitterDeliciousLinkedInGoogle BookmarksNetlogGoogle GmailMySpaceGoogle ReaderShare

, , , , , , , ,

Switch to our mobile site