line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#!/usr/bin/perl -w |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# dbrowcount.pm |
5
|
|
|
|
|
|
|
# Copyright (C) 2007-2015 by John Heidemann |
6
|
|
|
|
|
|
|
# $Id: 83fba9f283f462ac2039ab283fe5e131f7390a09 $ |
7
|
|
|
|
|
|
|
# |
8
|
|
|
|
|
|
|
# This program is distributed under terms of the GNU general |
9
|
|
|
|
|
|
|
# public license, version 2. See the file COPYING |
10
|
|
|
|
|
|
|
# in $dblibdir for details. |
11
|
|
|
|
|
|
|
# |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
package Fsdb::Filter::dbrowcount; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 NAME |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
dbrowcount - count the number of rows in an Fsdb stream |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=head1 SYNOPSIS |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
dbrowcount |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=head1 DESCRIPTION |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
Count the number of rows and write out a new fsdb file |
26
|
|
|
|
|
|
|
with one column (n) and one value: the number of rows. |
27
|
|
|
|
|
|
|
This program is a strict subset of L. |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
Although there are other ways to get a count of rows |
30
|
|
|
|
|
|
|
(C, or C and some processing), |
31
|
|
|
|
|
|
|
counting is so common it warrants its own command. |
32
|
|
|
|
|
|
|
(For example, consider how often C is used in regular shell |
33
|
|
|
|
|
|
|
scripting.) |
34
|
|
|
|
|
|
|
There are some gross and subtle differences, though, in that |
35
|
|
|
|
|
|
|
C doesn't require one to specify a column to search, |
36
|
|
|
|
|
|
|
and it also doesn't look for and skip null data items. |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=head1 OPTIONS |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
No program-specific options. |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=for comment |
43
|
|
|
|
|
|
|
begin_standard_fsdb_options |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
This module also supports the standard fsdb options: |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=over 4 |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=item B<-d> |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
Enable debugging output. |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=item B<-i> or B<--input> InputSource |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
Read from InputSource, typically a file name, or C<-> for standard input, |
56
|
|
|
|
|
|
|
or (if in Perl) a IO::Handle, Fsdb::IO or Fsdb::BoundedQueue objects. |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=item B<-o> or B<--output> OutputDestination |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
Write to OutputDestination, typically a file name, or C<-> for standard output, |
61
|
|
|
|
|
|
|
or (if in Perl) a IO::Handle, Fsdb::IO or Fsdb::BoundedQueue objects. |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=item B<--autorun> or B<--noautorun> |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
By default, programs process automatically, |
66
|
|
|
|
|
|
|
but Fsdb::Filter objects in Perl do not run until you invoke |
67
|
|
|
|
|
|
|
the run() method. |
68
|
|
|
|
|
|
|
The C<--(no)autorun> option controls that behavior within Perl. |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=item B<--help> |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
Show help. |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=item B<--man> |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
Show full manual. |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=back |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=for comment |
81
|
|
|
|
|
|
|
end_standard_fsdb_options |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=head1 SAMPLE USAGE |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=head2 Input: |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
#fsdb absdiff |
89
|
|
|
|
|
|
|
0 |
90
|
|
|
|
|
|
|
0.046953 |
91
|
|
|
|
|
|
|
0.072074 |
92
|
|
|
|
|
|
|
0.075413 |
93
|
|
|
|
|
|
|
0.094088 |
94
|
|
|
|
|
|
|
0.096602 |
95
|
|
|
|
|
|
|
# | /home/johnh/BIN/DB/dbrow |
96
|
|
|
|
|
|
|
# | /home/johnh/BIN/DB/dbcol event clock |
97
|
|
|
|
|
|
|
# | dbrowdiff clock |
98
|
|
|
|
|
|
|
# | /home/johnh/BIN/DB/dbcol absdiff |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head2 Command: |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
cat data.fsdb | dbrowcount |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=head2 Output: |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
#fsdb n |
107
|
|
|
|
|
|
|
6 |
108
|
|
|
|
|
|
|
# | /home/johnh/BIN/DB/dbrow |
109
|
|
|
|
|
|
|
# | /home/johnh/BIN/DB/dbcol event clock |
110
|
|
|
|
|
|
|
# | dbrowdiff clock |
111
|
|
|
|
|
|
|
# | /home/johnh/BIN/DB/dbcol absdiff |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=head2 Input 2: |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
As another example, this input produces the same output as above in |
116
|
|
|
|
|
|
|
C, but different output in C: |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
#fsdb absdiff |
119
|
|
|
|
|
|
|
- |
120
|
|
|
|
|
|
|
- |
121
|
|
|
|
|
|
|
- |
122
|
|
|
|
|
|
|
- |
123
|
|
|
|
|
|
|
- |
124
|
|
|
|
|
|
|
- |
125
|
|
|
|
|
|
|
# | /home/johnh/BIN/DB/dbrow |
126
|
|
|
|
|
|
|
# | /home/johnh/BIN/DB/dbcol event clock |
127
|
|
|
|
|
|
|
# | dbrowdiff clock |
128
|
|
|
|
|
|
|
# | /home/johnh/BIN/DB/dbcol absdiff |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=head1 SEE ALSO |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
L, |
134
|
|
|
|
|
|
|
L, |
135
|
|
|
|
|
|
|
L |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head1 CLASS FUNCTIONS |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=cut |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
@ISA = qw(Fsdb::Filter); |
142
|
|
|
|
|
|
|
($VERSION) = 2.0; |
143
|
|
|
|
|
|
|
|
144
|
1
|
|
|
1
|
|
5576
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
40
|
|
145
|
1
|
|
|
1
|
|
4
|
use Pod::Usage; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
151
|
|
146
|
|
|
|
|
|
|
|
147
|
1
|
|
|
1
|
|
8
|
use Fsdb::Filter; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
35
|
|
148
|
1
|
|
|
1
|
|
6
|
use Fsdb::IO::Reader; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
36
|
|
149
|
1
|
|
|
1
|
|
7
|
use Fsdb::IO::Writer; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
485
|
|
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=head2 new |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
$filter = new Fsdb::Filter::dbrowcount(@arguments); |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=cut |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
sub new { |
159
|
0
|
|
|
0
|
1
|
|
my $class = shift @_; |
160
|
0
|
|
|
|
|
|
my $self = $class->SUPER::new(@_); |
161
|
0
|
|
|
|
|
|
bless $self, $class; |
162
|
0
|
|
|
|
|
|
$self->set_defaults; |
163
|
0
|
|
|
|
|
|
$self->parse_options(@_); |
164
|
0
|
|
|
|
|
|
$self->SUPER::post_new(); |
165
|
0
|
|
|
|
|
|
return $self; |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=head2 set_defaults |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
$filter->set_defaults(); |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
Internal: set up defaults. |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=cut |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
sub set_defaults ($) { |
178
|
0
|
|
|
0
|
1
|
|
my($self) = @_; |
179
|
0
|
|
|
|
|
|
$self->SUPER::set_defaults(); |
180
|
0
|
|
|
|
|
|
$self->{_fscode} = undef; |
181
|
|
|
|
|
|
|
} |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=head2 parse_options |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
$filter->parse_options(@ARGV); |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
Internal: parse options |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
=cut |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
sub parse_options ($@) { |
192
|
0
|
|
|
0
|
1
|
|
my $self = shift @_; |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
$self->get_options( |
195
|
|
|
|
|
|
|
\@_, |
196
|
0
|
|
|
0
|
|
|
'help|?' => sub { pod2usage(1); }, |
197
|
0
|
|
|
0
|
|
|
'man' => sub { pod2usage(-verbose => 2); }, |
198
|
|
|
|
|
|
|
'autorun!' => \$self->{_autorun}, |
199
|
|
|
|
|
|
|
'close!' => \$self->{_close}, |
200
|
|
|
|
|
|
|
'F|fs|cs|fieldseparator|columnseparator=s' => \$self->{_fscode}, |
201
|
0
|
|
|
0
|
|
|
'i|input=s' => sub { $self->parse_io_option('input', @_); }, |
202
|
|
|
|
|
|
|
'log!' => \$self->{_logprog}, |
203
|
0
|
|
|
0
|
|
|
'o|output=s' => sub { $self->parse_io_option('output', @_); }, |
204
|
0
|
0
|
|
|
|
|
) or pod2usage(2); |
205
|
|
|
|
|
|
|
} |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=head2 setup |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
$filter->setup(); |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
Internal: setup, parse headers. |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
=cut |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
sub setup ($) { |
216
|
0
|
|
|
0
|
1
|
|
my($self) = @_; |
217
|
|
|
|
|
|
|
|
218
|
0
|
|
|
|
|
|
$self->finish_io_option('input', -comment_handler => $self->create_delay_comments_sub); |
219
|
0
|
|
|
|
|
|
my @output_options = (-cols => [qw(n)]); |
220
|
|
|
|
|
|
|
unshift (@output_options, -fscode => $self->{_fscode}) |
221
|
0
|
0
|
|
|
|
|
if (defined($self->{_fscode})); |
222
|
0
|
|
|
|
|
|
$self->finish_io_option('output', @output_options); |
223
|
|
|
|
|
|
|
} |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
=head2 run |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
$filter->run(); |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
Internal: run over all IO |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
=cut |
232
|
|
|
|
|
|
|
sub run ($) { |
233
|
0
|
|
|
0
|
1
|
|
my($self) = @_; |
234
|
|
|
|
|
|
|
|
235
|
0
|
|
|
|
|
|
my $read_fastpath_sub = $self->{_in}->fastpath_sub(); |
236
|
|
|
|
|
|
|
|
237
|
0
|
|
|
|
|
|
my $n = 0; |
238
|
0
|
|
|
|
|
|
my $fref; |
239
|
0
|
|
|
|
|
|
while ($fref = &$read_fastpath_sub()) { |
240
|
0
|
|
|
|
|
|
$n++; |
241
|
|
|
|
|
|
|
}; |
242
|
0
|
|
|
|
|
|
$self->{_out}->write_row_from_aref( [ $n ] ); |
243
|
|
|
|
|
|
|
} |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
=head2 finish |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
$filter->finish(); |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
Internal: write trailer. |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
=cut |
252
|
|
|
|
|
|
|
#sub finish ($) { |
253
|
|
|
|
|
|
|
# my($self) = @_; |
254
|
|
|
|
|
|
|
# |
255
|
|
|
|
|
|
|
# $self->SUPER::finish(); |
256
|
|
|
|
|
|
|
#} |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
=head1 AUTHOR and COPYRIGHT |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
Copyright (C) 2007-2015 by John Heidemann |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
This program is distributed under terms of the GNU general |
263
|
|
|
|
|
|
|
public license, version 2. See the file COPYING |
264
|
|
|
|
|
|
|
with the distribution for details. |
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
=cut |
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
1; |
269
|
|
|
|
|
|
|
|