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
#!/usr/bin/env perl use strict; use warnings; use feature qw(say); my $ts = <DATA>; my @flags = (true=> (<DATA> =~ /(false|true)/g)); my @header = (<DATA> =~ /([a-z_]+)/g); use YAML::XS; print Dump \@header, \@flags; my @selected = grep $flags[ $_ ] eq 'false', 0 .. $#flags; my %output_fh; for my $i (@selected) { my $fname = "file$i"; open my $fh, '>', $fname or die "Cannot open '$fname' for writing: $!"; say $fh $header[$i]; $output_fh{$i} = $fh; } while (my $line = <DATA>) { next unless $line =~ /\S/; # next if $line =~ /^false/; my @cols = split qr{ , }x, $line; for my $i (@selected) { say { $output_fh{$i} } join(q{ : }, $cols[0], $cols[$i]); } }
1
2
3
4
5
6
7
8
9
------------------------------------DATA.csv-----------------------------------------------
2015.07.23 16:47:23
false, false, true, false, true, false,
defau, ame_a, ame_m, ame_uc, ame_f, ams_l, en_affi,
true DPA01, fault, fault, fault, fault, fault, 1,
false DPA02, , , , , fault, 1,
false DPA03, fault, fault, olt, fault, fault, 1,
false DPA01, fault, at, fault, 2, fault, 1,
1
2
3
4
5
6
---------------------------------ame_a-----------------------------------
ame_a,
fault,
,
fault,
fault,
1
2
3
4
5
6
------------------------------ame_m-------------------------------------
ame_m,
fault,
,
fault,
at,
1
2
3
4
5
6
----------------------------------ame_f-----------------------------------
ame_f,
fault,
,
fault,
2,
1
2
3
4
5
6
--------------------------------en_affi----------------------------------
en_affi,
1,
1,
1,
1,
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
# Kommandozeile; Ausgabedateien kontrollieren
$ for x in file?; do echo "--- $x ------"; cat $x; echo; done
--- file1 ------
ame_a
fault
fault
fault
--- file2 ------
ame_m
fault
fault
at
--- file4 ------
ame_f
fault
fault
2
--- file6 ------
en_affi
1
1
1
1
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
# Kommandozeile; Ausgabedateien kontrollieren
$ for x in *_* ; do echo "--- $x ------"; cat $x; echo; done
--- ame_a ------
ame_a
fault,
,
fault,
fault,
--- ame_f ------
ame_f
fault,
,
fault,
2,
--- ame_m ------
ame_m
fault,
,
fault,
at,
--- en_affi ------
en_affi
1,
1,
1,
1,
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
# Kommandozeile; Ausgabedateien kontrollieren
$ for x in *_* ; do echo "--- $x ------"; cat $x; echo; done
--- ame_a ------
ame_a
fault,
,
fault,
fault,
--- ame_f ------
ame_f
fault,
,
fault,
2,
--- ame_m ------
ame_m
fault,
,
fault,
at,
--- en_affi ------
en_affi
1,
1,
1,
1,
1
2
3
4
5
6
7
8
9
10
11
------------------------------------DATA.csv---------------------------------
2015.07.23 16:47:23
false, false, true, false, true, false,
defau, ame_a, ame_m, ame_uc, ame_f, ams_l, en_affi,
true DPA01, fault, fault, fault, fault, fault, 1,
false DPA02, , , , , fault, 1,
true DPA03, fault, fault, olt, fault, fault, 1,
false DPA01, fault, at, fault, 2, fault, 1,
false DPA02, , , , , fault, 1,
true DPA03, fault, fault, olt, fault, fault, 1,
DPA02, , , , , fault, 1,
DPA01, fault, at, fault, 2, fault, 1,
DPA02, , , , , fault, 1,
Guest HenriAls allgemeine Frage für meine eigene Forschung
ich möchte mich beschätigen, und jetzt möchte ich alle zeile mit "false" haben.
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
#!/usr/bin/perl use strict; use warnings; use autodie; use List::Util 'any'; my $input_file = 'DATA.csv'; my (@headers, %docs); my $n = 0; my @docs; open my $fh, '>',"fh.csv"; open(my $Fhresult, '<', $input_file); while (<$Fhresult> ) { last if (/^\d+\s+\d{2}:\d{2}:\d{2}\s*$/); } while (<$Fhresult> ) { if ($. == 3) { chomp; @headers = split ( /[ ,. ;:\(\)\/\*\"]+/ ); next; } next unless /\S/; chomp; my ($file, @fields) = split /\s*,\s*|\s+/; push @docs, {'file'=>$file, 'fields'=>\@fields} if any { $_ eq 'false' } @fields; } close($Fhresult);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#!/usr/bin/perl use warnings; use strict; my @a = <DATA>; my @b = grep(/false/, @a); chomp(@b); for (my $i=0; $i <= $#b; $i++) { print $i + 1; print ". "; print "$b[$i]\n"; } __DATA__ 2015.07.23 16:47:23 false, false, true, false, true, false, defau, ame_a, ame_m, ame_uc, ame_f, ams_l, en_affi, true DPA01, fault, fault, fault, fault, fault, 1, false DPA02, , , , , fault, 1, true DPA03, fault, fault, olt, fault, fault, 1, false DPA01, fault, at, fault, 2, fault, 1, false DPA02, , , , , fault, 1, true DPA03, fault, fault, olt, fault, fault, 1,
Guest Henrisagt mal hlubenow, kannst du bitte mir mal nur sagen , ob ich ein bisschen auf dem weg bin, wenn du dir diesen Anfang anschauen?
Guest HenriJa, deine lösung ist sicherlich korrekt, du bist schon ein Profi, habe ich schon viele von dir hier gelesen.