-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsplit
More file actions
executable file
·216 lines (183 loc) · 3.95 KB
/
split
File metadata and controls
executable file
·216 lines (183 loc) · 3.95 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/perl
# $Id: split,v 1.16 2006-08-27 13:43:03 jonathan Exp $
# Written by Jonathan Ledlie
use strict;
use POSIX qw(floor ceil);
use Getopt::Std;
my $usage = "Usage: split -k 'operators' [-z] filename\n";
$usage .= " cat filename | split -k 'operators'\n\n";
$usage .= '\'operators\': e.g. $1 $4+$5+$6 7+5 $5>0'."\n";
$usage .= " -z convert any bad eval to 0 (default is to ignore the line)\n";
$usage .= " Operators may be boolean, in which case they are not printed\n";
$usage .= " -e call eval on every item (useful for e.g. floor(x*y))\n";
$usage .= " -q quiet mode";
#e.g. $1 $4+5 $2-$4 $4+$3*4
my $debug = 0;
my $opsLine = '';
my $zero = '';
my $ignoreLineCount = 0;
my $validLineCount = 0;
my $evalAll = 0;
my %para = ();
getopts('wk:zeq', \%para);
if (defined($para{'k'})) {
$opsLine = $para{'k'};
} else {
die ($usage);
}
if (defined($para{'e'})) {
$evalAll = 1;
}
my $convertToZero = 0;
if (defined($para{'z'})) {
$convertToZero = 1;
}
my @ops = split (/\s+/, $opsLine);
#see if this can be fast pathed
if ($#ops==0 && !&containsBoolean($ops[0]) &&
!&containsMath($ops[0])) {
if ($ops[0] =~ /\$(\d+)/) {
if ($debug) {
warn "fast path\n";
}
#exit (0);
my $item = $1;
$item--;
while (my $line = <>) {
my (@items) = split (/\s+/, $line);
#print "item = $item\n";
print "$items[$item]\n";
#exit (0);
}
} else {
die ("Problem with fast path\n");
}
exit (0);
}
while (my $line = <>) {
$line =~ s/^\s+//;
$line =~ s/\s+$//;
my (@items) = split (/\s+/, $line);
my $lineOK = 1;
my $resLine = '';
for (my $i = 0; $i <= $#ops; $i++) {
my $isBoolean = 0;
my $op = $ops[$i];
# Could be done with this, but you would lose error checking
# $op =~ s/(\d+)/$items[$1]/g;
while ($lineOK && $op =~ /\$(\d+)/) {
my $column = $1;
if ($debug) {
warn "column $column\n";
}
if (defined ($items[$column-1])) {
if ($debug) {
warn "got col $column $items[$column-1]\n";
}
$op =~ s/\$$column/$items[$column-1]/;
# print ("op $op\n");
} else {
if ($debug) {
warn "no col $column\n";
}
#warn ("Line did not match: $line operator $ops[$i]\n");
$lineOK = 0;
}
}
if ($debug) {
warn "op $op\n";
}
if (&containsBoolean($op)) {
if ($debug) {
warn "contains bool $op\n";
}
#eval $op;
#warn (eval {$op});
#warn "op $op\n";
if (!(eval $op)) {
if ($debug) {
warn "false\n";
}
if ($convertToZero) {
$op = 0;
} else {
$lineOK = 0;
}
} else {
if ($debug) {
warn "true\n";
}
}
} else {
if (&containsMath($op)) {
if ($debug) {
warn "contains math op $op\n";
}
$op = eval ($op);
if ($op =~ /e/) {
my $math_op = sprintf ("%.3f",$op);
$op = $math_op;
}
}
if ($i != 0) {
$resLine .= ' ';
}
$resLine = $resLine . "$op";
}
if ($debug) {
warn "resLine $resLine\n";
}
}
if ($lineOK) {
#$resLine =~ s/^\s+//;
print "$resLine\n";
$validLineCount++;
} else {
$ignoreLineCount++;
#warn "Ignoring: $line";
}
#print "done with line\n";
#exit (0);
}
if ($ignoreLineCount > 0 && !defined($para{'q'})) {
warn ("split ignored: $ignoreLineCount valid: $validLineCount lines\n");
}
sub containsBoolean {
my ($val) = @_;
if (($val =~ /==/) ||
($val =~ /<=/) ||
($val =~ />=/) ||
($val =~ /</) ||
($val =~ />/)) {
return 1;
}
return 0;
}
sub containsMath {
my ($val) = @_;
$val =~ s/^-//;
#print "math?\n";
if ($val =~ /[A-Za-z]/) {
#print "no 1\n";
return 0;
}
if (($val =~ /\d\+\d/) ||
($val =~ /\d\-\d/) ||
($val =~ /\d\*\d/) ||
($val =~ /\d\/\d/) ||
($val =~ /\d\^\d/)) {
#print "yes\n";
return 1;
}
if (($val =~ /\+/) ||
($val =~ /\-/) ||
($val =~ /\*/) ||
($val =~ /\//) ||
($val =~ /\^/)) {
#print "yes\n";
return 1;
}
#print "no 2\n";
return 0;
}