-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpcsv
executable file
·152 lines (132 loc) · 3.03 KB
/
pcsv
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env perl
#####################################
#
# Author: David Klein
#
# Contact: david@soinkleined.com
#
# Requires: delimited file with column headers
#
# Version: 1.1 - 2014-03-07 - David Klein <david@soinkleined.com>
# * Added field length formatting
# * Added sample csv
# * Corrected help output
#
# Version: 1.0 - 2014-02-19 - David Klein <david@soinkleined.com>
# * Initial release
#
#####################################
use strict;
use File::Basename;
use Getopt::Long qw(:config no_ignore_case bundling);
my $InFile='sample.csv';
my $altInFile;
my @entry;
my $found=0;
my $field=0;
my $debug=0;
my $csv=0;
my $number=0;
my $cmd=basename($0);
my $version='1.1';
GetOptions ('d|debug!' => \$debug,
'c|csv!' => \$csv,
'f|file=s' => \$altInFile,
'F|field=i' => \$field,
'h|help' => \&help,
'p|number!' => \$number,
'V|version' => \&version
);
my $argString=shift;
my $searchString;
&prterr("No argument given! Please use -h for help.", "1") unless $argString;
if ($altInFile){
$InFile=$altInFile;
}
# Allow for glob style searches
# Other string combos need to be escaped or they'll break the search
if ($argString =~ /\*/){
$argString =~ s/\*/\.\*/g;
}
$searchString=$argString;
open FILE, "$InFile" || &prterr("Cannot open \"$InFile\" : $!","1");
chomp(my $line=<FILE>);
$line =~ s/\"//g;
if ($csv){
print $line."\n";
}
my @fields=split(/,/,$line); # Current delimiter is "," <- change from ", "
my $length;
for (@fields){
my $elementLength=length($_);
if ($elementLength > $length){
$length=$elementLength;
}
}
my $lineCounter=0;
$field-- if $field;
while (<FILE>){
s/\"//g;
$lineCounter++;
next if (! $field && ! /^$searchString,/i);
#Convert field number to Array element
chomp(@entry=split(/,/,$_));
if ($entry[$field] =~ /^$searchString$/i){
$found++;
if ($csv) {
print;
}else {
my $i=0;
for (@fields){
printf("%2d: ", $i+1) if $number;
printf("%-$length"."s : %s\n", $_, $entry[$i]);
$i++;
}
}
}
}
close FILE;
if ($debug){
print <<EOF;
+----------------------------------------
+ DEBUG
+----------------------------------------
+ Matching Entries: $found
+ Number of Entries: $lineCounter
+ Filename: $InFile
+ Search Field: $fields[$field]
+ Search String: /^$searchString\$/i
+----------------------------------------
EOF
}
&prterr("$argString not found!", "1") unless ($found);
sub prterr{
my $Message=shift;
my $ExitCode=shift;
print "<E> $Message\n";
exit $ExitCode;
}
sub help{
print <<EOF;
Usage: $cmd [-cdp] string
$cmd [-cdp] -F n string
$cmd -V
Queries information in $InFile
and displays it in a readable format.
Arguments:
-c, --csv print CSV header
-d, --debug print debugging information
-f, --file parse file
-F, --field search field n
-p, --number prepend field numbers to output
-V, --version output version information and exit
Report bugs to <david\@soinkleined.com>
EOF
exit 0;
}
sub version{
print <<EOF;
$version
EOF
exit 0;
}