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
3681 OUT 2014-05-06 12:57:05 22 30 3338359406 3338753402 unkown 8414
3681 OUT 2014-05-06 12:39:44 37 43 3338359406 3338753402 unkown 8414
3681 OUT 2014-05-06 11:56:15 191 210 3338359406 3338753402 unkown 3991110
3681 OUT 2014-05-06 12:06:15 0 10 3338359406 3338753402 unkown 033372270
5494 OUT 2014-05-06 06:25:48 149 172 3375550834 X international 0038763931307
5494 OUT 2014-05-06 07:43:49 419 443 3375585134 X international 0038763649266
5494 OUT 2014-05-06 11:41:07 127 141 3375585134 X international 002207733514
5494 OUT 2014-05-06 08:54:48 252 259 3375550834 X international 00447589972757
5494 OUT 2014-05-06 01:28:17 0 11 3375585133 X international 0023221350752
5494 OUT 2014-05-06 11:41:15 121 133 3375550834 X international 0022062892015
5494 OUT 2014-05-06 01:28:25 0 10 3375585133 X international 005331997555
5494 OUT 2014-05-06 01:28:39 0 12 3375585133 X international 002392204144
5494 OUT 2014-05-06 08:15:03 0 14 3375585133 X international 0038763456569
5494 OUT 2014-05-06 08:15:02 0 14 3375585134 X international 0038763649266
5494 OUT 2014-05-06 11:41:09 122 131 3375585133 X international 0022062892015
5494 OUT 2014-05-06 08:14:36 0 14 3375585134 X international 0038763649266
5494 OUT 2014-05-06 01:29:00 0 10 3375585133 X international 0037125050148
7787 OUT 2014-05-06 04:06:37 125 132 3376290890 X international 00447589972819
7787 OUT 2014-05-06 04:06:51 124 131 3376290890 X international 00447589972819
7787 OUT 2014-05-06 04:06:58 127 134 3376290890 X international 00447589972819
7787 OUT 2014-05-06 07:24:22 121 128 3376290890 X international 00447589972819
7787 OUT 2014-05-06 08:44:07 126 133 3376290890 X international 00447589972819
7787 OUT 2014-05-06 04:07:26 119 126 3376290890 X international 00447589972819
7787 OUT 2014-05-06 04:07:40 122 129 3376290890 X international 00447589972819
7787 OUT 2014-05-06 10:48:55 127 146 3376290890 X international 002207396071
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 43 44 45 46 47 48 49 50
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @data; # Das Array, das Du schon erzeugst my %id_count; # Hash: zu jeder ID (die Zahlen am Zeilenanfang) die Anzahl der Vorkommen while (my $row = <DATA>) { chomp $row; push @data, $row; my ($id) = ($row =~ /^(\d+)/); $id_count{$id}++; } print Dumper \%id_count; __DATA__ 3681 OUT 2014-05-06 12:57:05 22 30 3338359406 3338753402 unkown 8414 3681 OUT 2014-05-06 12:39:44 37 43 3338359406 3338753402 unkown 8414 3681 OUT 2014-05-06 11:56:15 191 210 3338359406 3338753402 unkown 3991110 3681 OUT 2014-05-06 12:06:15 0 10 3338359406 3338753402 unkown 033372270 5494 OUT 2014-05-06 06:25:48 149 172 3375550834 X international 0038763931307 5494 OUT 2014-05-06 07:43:49 419 443 3375585134 X international 0038763649266 5494 OUT 2014-05-06 11:41:07 127 141 3375585134 X international 002207733514 5494 OUT 2014-05-06 08:54:48 252 259 3375550834 X international 00447589972757 5494 OUT 2014-05-06 01:28:17 0 11 3375585133 X international 0023221350752 5494 OUT 2014-05-06 11:41:15 121 133 3375550834 X international 0022062892015 5494 OUT 2014-05-06 01:28:25 0 10 3375585133 X international 005331997555 5494 OUT 2014-05-06 01:28:39 0 12 3375585133 X international 002392204144 5494 OUT 2014-05-06 08:15:03 0 14 3375585133 X international 0038763456569 5494 OUT 2014-05-06 08:15:02 0 14 3375585134 X international 0038763649266 5494 OUT 2014-05-06 11:41:09 122 131 3375585133 X international 0022062892015 5494 OUT 2014-05-06 08:14:36 0 14 3375585134 X international 0038763649266 5494 OUT 2014-05-06 01:29:00 0 10 3375585133 X international 0037125050148 7787 OUT 2014-05-06 04:06:37 125 132 3376290890 X international 00447589972819 7787 OUT 2014-05-06 04:06:51 124 131 3376290890 X international 00447589972819 7787 OUT 2014-05-06 04:06:58 127 134 3376290890 X international 00447589972819 7787 OUT 2014-05-06 07:24:22 121 128 3376290890 X international 00447589972819 7787 OUT 2014-05-06 08:44:07 126 133 3376290890 X international 00447589972819 7787 OUT 2014-05-06 04:07:26 119 126 3376290890 X international 00447589972819 7787 OUT 2014-05-06 04:07:40 122 129 3376290890 X international 00447589972819 7787 OUT 2014-05-06 10:48:55 127 146 3376290890 X international 002207396071
1 2 3 4
my ($id,undef,undef,undef,undef,$count) = (split/\s+/,$row); # Zeile nach Leerzeichen in mehrere Felder aufsplitten # undef bedeutet hier: Ergebnis des split wegwerfen, also: unwichtige Spalte wegwerfen $id_count{$id}{anz}++; # Auftreten des IDs zählen $id_count{$id}{sum} += $count; # sechste Spalte des IDs aufsummieren
1 2 3 4
printf # formatiert ausgeben '%-6d %-6d %-6d\n', # alle Felder 6 Zeichen breit, linksbündig $_,$id_count{$_}{anz},$id_count{$_}{sum} # Werte der Felder for keys %id_count; # für alle Schlüssel im Hash
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 43 44 45 46 47 48 49 50 51 52 53 54
#!/usr/bin/perl use warnings; use strict; sub prepareLine { my $a = shift; $a =~ s/ +/ /g; return $a; } my @data = <DATA>; my $i; my $u; my @a; my %id_count; my %six_count; my $sep = " "; foreach $i (@data) { chomp($i); $i = &prepareLine($i); @a = split($sep, $i); $id_count{$a[0]}++; $six_count{$a[0]} += $a[5]; } for $i (sort(keys(%id_count))) { print "$i\t$id_count{$i}\t$six_count{$i}\n"; } __DATA__ 3681 OUT 2014-05-06 12:57:05 22 30 3338359406 3338753402 unkown 8414 3681 OUT 2014-05-06 12:39:44 37 43 3338359406 3338753402 unkown 8414 3681 OUT 2014-05-06 11:56:15 191 210 3338359406 3338753402 unkown 3991110 3681 OUT 2014-05-06 12:06:15 0 10 3338359406 3338753402 unkown 033372270 5494 OUT 2014-05-06 06:25:48 149 172 3375550834 X international 0038763931307 5494 OUT 2014-05-06 07:43:49 419 443 3375585134 X international 0038763649266 5494 OUT 2014-05-06 11:41:07 127 141 3375585134 X international 002207733514 5494 OUT 2014-05-06 08:54:48 252 259 3375550834 X international 00447589972757 5494 OUT 2014-05-06 01:28:17 0 11 3375585133 X international 0023221350752 5494 OUT 2014-05-06 11:41:15 121 133 3375550834 X international 0022062892015 5494 OUT 2014-05-06 01:28:25 0 10 3375585133 X international 005331997555 5494 OUT 2014-05-06 01:28:39 0 12 3375585133 X international 002392204144 5494 OUT 2014-05-06 08:15:03 0 14 3375585133 X international 0038763456569 5494 OUT 2014-05-06 08:15:02 0 14 3375585134 X international 0038763649266 5494 OUT 2014-05-06 11:41:09 122 131 3375585133 X international 0022062892015 5494 OUT 2014-05-06 08:14:36 0 14 3375585134 X international 0038763649266 5494 OUT 2014-05-06 01:29:00 0 10 3375585133 X international 0037125050148 7787 OUT 2014-05-06 04:06:37 125 132 3376290890 X international 00447589972819 7787 OUT 2014-05-06 04:06:51 124 131 3376290890 X international 00447589972819 7787 OUT 2014-05-06 04:06:58 127 134 3376290890 X international 00447589972819 7787 OUT 2014-05-06 07:24:22 121 128 3376290890 X international 00447589972819 7787 OUT 2014-05-06 08:44:07 126 133 3376290890 X international 00447589972819 7787 OUT 2014-05-06 04:07:26 119 126 3376290890 X international 00447589972819 7787 OUT 2014-05-06 04:07:40 122 129 3376290890 X international 00447589972819 7787 OUT 2014-05-06 10:48:55 127 146 3376290890 X international 002207396071
2014-05-06T16:09:38 Linuxerbitte $a und $b nicht "fremdnutzen".
Die sind global fürs Sortieren vordefiniert und sollten nicht für anderes verwendet werden.
LinuxerWarum veränderst Du aufwendig die Datenzeile? split() verarbeitet doch Regex-Pattern.
2014-05-06T18:08:38 hlubenowBeides hängt mit meiner Vorstellung zusammen, möglichst allgemein zu schreiben und sprachspezifische Konstrukte möglichst zu vermeiden.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
function split(s, plain_sep) local stringtable = {} local start = 1 local found = string.find(s, plain_sep, start) while found ~= nil do table.insert(stringtable, string.sub(s, start, found - 1)) start = found + string.len(plain_sep) found = string.find(s, plain_sep, start) end if start <= string.len(s) then table.insert(stringtable, string.sub(s, start)) end return stringtable end
1 2 3 4 5 6 7 8 9
#!/usr/bin/perl use warnings; use strict; my @a = (1, 2, 3); foreach my $i (@a) { print "$i\n"; }
2014-05-07T22:18:56 hlubenowAn foreach habe ich mich also (notgedrungen) gewöhnt.