Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
aix-pm
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Gerhard Gonter
aix-pm
Commits
9eca9c38
Commit
9eca9c38
authored
7 years ago
by
Gerhard Gonter
Browse files
Options
Downloads
Patches
Plain Diff
added option --in to search for multiple values
parent
81ffadc7
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
modules/util/csv.pl
+67
-11
67 additions, 11 deletions
modules/util/csv.pl
with
67 additions
and
11 deletions
modules/util/csv.pl
+
67
−
11
View file @
9eca9c38
...
...
@@ -62,6 +62,12 @@ print list of column names (CSV header)
only display this many items
=head2 searching
--select <field>=<value>
--find <pattern>
--in <field> <value-1> .. <value-n>
=cut
use
strict
;
...
...
@@ -91,16 +97,21 @@ my $view= 'matrix'; # values: matrix, extended, header, json, dumper
my
$all
=
0
;
# for extend view, sofar...
my
$find_pattern
=
undef
;
# this is used for a pattern match
my
$search_string
=
undef
;
# this is used to select a certain value in a column
# used for option --in <fieldname> <field_value>+
my
@search_strings
;
my
$search_field_name
=
my
$max_items
=
undef
;
sub
set_utf8
{
$UTF8
=
1
;
binmode
(
STDIN
,
'
:utf8
');
binmode
(
STDOUT
,
'
:utf8
');
}
sub
usage
{
system
("
perldoc '$0'
");
exit
(
0
);
}
my
@PAR
=
();
my
@PAR
S
=
();
while
(
defined
(
my
$arg
=
shift
(
@ARGV
)))
{
if
(
$arg
eq
'
--
')
{
push
(
@PAR
,
@ARGV
);
@ARGV
=
();
}
elsif
(
$arg
eq
'
-
')
{
push
(
@PAR
,
$arg
);
}
if
(
$arg
eq
'
--
')
{
push
(
@PAR
S
,
@ARGV
);
@ARGV
=
();
}
elsif
(
$arg
eq
'
-
')
{
push
(
@PAR
S
,
$arg
);
}
elsif
(
$arg
=~
/^--(.+)/
)
{
my
(
$opt
,
$val
)
=
split
('
=
',
$
1
,
2
);
...
...
@@ -114,6 +125,10 @@ while (defined (my $arg= shift (@ARGV)))
$search_string
=
$val
||
shift
(
@ARGV
);
# print __LINE__, " search_string=[$search_string]\n";
}
elsif
(
$opt
eq
'
in
')
{
$search_field_name
=
$val
||
shift
(
@ARGV
);
}
elsif
(
$opt
eq
'
max
')
{
$max_items
=
$val
||
shift
(
@ARGV
);
}
elsif
(
$opt
eq
'
hdr
')
{
$view
=
'
header
';
}
elsif
(
$opt
eq
'
json
')
{
$view
=
'
json
';
}
...
...
@@ -160,12 +175,19 @@ while (defined (my $arg= shift (@ARGV)))
}
else
{
push
(
@PAR
,
$arg
);
if
(
defined
(
$search_field_name
))
{
push
(
@search_strings
,
$arg
);
}
else
{
push
(
@PARS
,
$arg
);
}
}
}
unless
(
@PAR
)
unless
(
@PAR
S
)
{
=begin comment
...
...
@@ -178,7 +200,7 @@ EOX
=end comment
=cut
push
(
@PAR
,
'
-
');
push
(
@PAR
S
,
'
-
');
}
my
$csv
=
new
Util::
Simple_CSV
('
separator
'
=>
$CSV_SEP
,
...
...
@@ -277,17 +299,51 @@ EOX
$csv
->
set
('
fidef
'
=>
\
&fidef2
);
}
elsif
(
@search_strings
)
{
# the filter is dynamically generated since the field number is only
# known after the column names are identified!
sub
fidef3
{
my
$obj
=
shift
;
# print __LINE__, " in fidef2\n";
my
$cols
=
$obj
->
{'
columns
'};
my
$col
=
0
;
my
%cols
=
map
{
$_
=>
$col
++
}
@$cols
;
# print "cols: ", Dumper ($cols);
# print "cols: ", Dumper (\%cols);
my
%fidef3
=
map
{
$_
=>
1
}
@search_strings
;
print
STDERR
__LINE__
,
"
fidef3:
",
main::
Dumper
(
\
%fidef3
);
my
$sub
=
<<"EOX";
my \$fidef= sub
{
my \$row= shift;
return (exists (\$fidef3{\$row->[$cols{$search_field_name}]}));
};
EOX
print
STDERR
"
sub: [
$sub
]
\n
";
# if ($debug_level ... );
my
$res
=
eval
(
$sub
);
# print "res=[$res]\n";
$res
;
};
$csv
->
set
('
fidef
'
=>
\
&fidef3
);
}
if
(
defined
(
$max_items
))
{
$csv
->
set
(
max_items
=>
$max_items
);
}
my
$fnm
=
shift
(
@PAR
);
my
$fnm
=
shift
(
@PAR
S
);
$csv
->
load_csv_file
(
$fnm
);
# print "csv: ", Dumper ($csv); exit (0);
while
(
my
$fnm
=
shift
(
@PAR
))
while
(
my
$fnm
=
shift
(
@PAR
S
))
{
$csv
->
merge_csv_file
(
$fnm
);
}
...
...
@@ -342,7 +398,7 @@ elsif ($view eq 'dumper')
}
elsif
(
$view
eq
'
no
')
{
# dont show anyhing
# dont show any
t
hing
}
else
{
...
...
@@ -374,7 +430,7 @@ __END__
=head1 Copyright
Copyright (c) 2006..201
3
Gerhard Gonter. All rights reserved. This
Copyright (c) 2006..201
8
Gerhard Gonter. All rights reserved. This
is free software; you can redistribute it and/or modify it under
the same terms as Perl itself.
...
...
@@ -408,5 +464,5 @@ Together with tagging, this would be a powerful feature.
Also: Add a method to tag (not just filter) rows via callback...
Uh... that's getting comp
p
lex!
Uh... that's getting complex!
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment