bin/cut

filter: Statementstmt 75.0% 78 / 104 Branchbran 61.1% 33 / 54 Conditioncond 77.7% 7 / 9 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 90.0% 9 / 10 Podpod n/a totaltotal 71.0% 130 / 183 CC 30 SCAR 24.3
CC
30
Cov
71%
parse_fields
18.3
handle_f
12.0
handle_b
6.5
CRAP 11.3
« bin/date ↑ Summary bin/nl »
1 #!/usr/bin/perl
2  
3 =begin metadata
4  
5 Name: cut
6 Description: select portions of each line of a file
7 Author: Rich Lafferty, rich@alcor.concordia.ca
8 License: perl
9  
10 =end metadata
11  
12 =cut
13  
14 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 6 ▶use File::Basename qw(basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 6 ▶use Getopt::Std qw(getopts);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 6 ▶use constant EX_SUCCESS => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 6 ▶use constant EX_FAILURE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 6 my $me = basename($0);
23  
24 6 my %opt;
25 6 ▶getopts('b:c:d:f:ns', \%opt) or usage();
Branch: 1 branch
BranchTrueFalseTotal
&getopts("b:c:d:f:ns", \%opt) or usage() 6 0 6
26  
27 # There's no difference between -b and -c on any unix I
28 # use regularly -- it's for i18n. Thus, -n is a noop, too.
29 6 ▶if (defined $opt{'b'}) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $opt{'b'}) { } 3 3 6
elsif (defined $opt{'c'}) { } 0 3 3
elsif (defined $opt{'f'}) { } 3 0 3
30 3 ▶ usage() if $opt{'f'};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"f"} 0 3 3
31 3 handle_b($opt{'b'});
32 }
33 elsif (defined $opt{'c'}) {
34 0 ▶ usage() if $opt{'f'};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"f"} 0 0 0
35 0 handle_b($opt{'c'});
36 }
37 elsif (defined $opt{'f'}) {
38 3 handle_f($opt{'f'}, $opt{'d'}, $opt{'s'});
39 }
40 else {
41 0 warn "$me: byte, character or field list required\n";
42 0 usage();
43 }
44 6 exit EX_SUCCESS;
45  
46 8 ▶sub checknum {
Subroutine checknum: called CC=3 SCAR=15.0
47 8 my $n = shift;
48 8 ▶ if ($n !~ m/\A\-?[0-9]+\Z/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($n =~ /\A\-?[0-9]+\Z/) 0 8 8
49 0 warn "$me: unexpected byte or field number: '$n'\n";
50 0 exit EX_FAILURE;
51 }
52 8 ▶ if ($n == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n == 0) 0 8 8
53 0 warn "$me: bytes and fields are numbered from 1\n";
54 0 exit EX_FAILURE;
55 }
56 }
57  
58 6 ▶sub parse_fields {
Subroutine parse_fields: called CC=15 SCAR=29.1
59 6 my $spec = shift;
60 6 my @list = split /,/, $spec;
61  
62 6 my $to_end;
63 my @cols;
64 6 foreach my $item (@list) {
65 6 my ($from, $is_range, $to);
66 6 ▶ if ($item =~ m/\A([0-9]*)(\-?)([0-9]*)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item =~ /\A([0-9]*)(\-?)([0-9]*)\z/) { } 6 0 6
67 6 $from = $1;
68 6 $is_range = $2;
69 6 $to = $3;
70 }
71 else {
72 0 warn "$me: invalid byte list: '$item'\n";
73 0 exit EX_FAILURE;
74 }
75 6 ▶ checknum($from) if length($from);
Branch: 1 branch
BranchTrueFalseTotal
if length $from 4 2 6
76 6 ▶ checknum($to) if length($to);
Branch: 1 branch
BranchTrueFalseTotal
if length $to 4 2 6
77 6 ▶ if (!length($from) && !length($to)) { # reject lone '-'
Branch: 1 branch
BranchTrueFalseTotal
unless (length $from or length $to) 0 6 6
Condition66%2/3
length $from or length $to
l!l&&r!l&&!r
420
MC/DC0%0/2
length $from or length $to
length $from length $to
Truth table66%2/3
A or B
A= length $fromB= length $to
ABresult
1X1
011
000
78 0 warn "$me: invalid byte list\n";
79 0 exit EX_FAILURE;
80 }
81 6 ▶ if (length($from) && length($to)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $from and length $to) 2 4 6
Condition100%3/3
length $from and length $to
!ll&&!rl&&r
222
MC/DC100%2/2
length $from and length $to
length $from length $to
Truth table100%3/3
A and B
A= length $fromB= length $to
ABresult
0X0
100
111
82 2 ▶ if ($from > $to) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from > $to) 0 2 2
83 0 warn "$me: invalid range $from-$to\n";
84 0 exit EX_FAILURE;
85 }
86 2 ▶ $is_range = 0 if $from == $to;
Branch: 1 branch
BranchTrueFalseTotal
if $from == $to 0 2 2
87 }
88 6 ▶ if ($is_range) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_range) { } 4 2 6
89 4 ▶ if (length($from) && length($to)) { # a-b
Branch: 2 branches
BranchTrueFalseTotal
if (length $from and length $to) { } 2 2 4
elsif (length $from) { } 0 2 2
Condition66%2/3
length $from and length $to
!ll&&!rl&&r
202
MC/DC50%1/2
length $from and length $to
length $from length $to
Truth table66%2/3
A and B
A= length $fromB= length $to
ABresult
0X0
100
111
90 2 push @cols, $from .. $to;
91 }
92 elsif (length $from) { # a-
93 0 push @cols, $from;
94 0 $to_end = 1;
95 }
96 else { # -a
97 2 push @cols, 1 .. $to;
98 }
99 }
100 else {
101 2 push @cols, $from;
102 }
103 }
104 6 my @sorted = sort { $a <=> $b } @cols;
105 6 unshift @sorted, $to_end;
106 6 return @sorted;
107 }
108  
109 3 ▶sub handle_b {
Subroutine handle_b: called CC=6 SCAR=18.7
110 3 my $spec = shift;
111 3 my ($to_end, @cols) = parse_fields($spec);
112  
113 3 ▶ while (<>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline ARGV)) 3 3 6
114 3 chomp;
115 3 my $col = 0;
116 3 my @chars = split //;
117 3 foreach my $c (@cols) {
118 6 ▶ next if $c <= $col;
Branch: 1 branch
BranchTrueFalseTotal
if $c <= $col 0 6 6
119 6 print $chars[$c - 1];
120 6 $col = $c;
121 }
122 3 ▶ if ($to_end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to_end) 0 3 3
123 0 $col++;
124 0 foreach my $c ($col .. scalar(@chars)) {
125 0 print $chars[$c - 1];
126 }
127 }
128 3 print "\n";
129 }
130 }
131  
132 3 ▶sub handle_f {
Subroutine handle_f: called CC=9 SCAR=24.8
133 3 my ($spec, $delim, $sflag) = @_;
134  
135 3 my ($to_end, @cols) = parse_fields($spec);
136 3 ▶ if (defined $delim) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $delim) { } 3 0 3
137 3 $delim = substr $delim, 0, 1;
138 }
139 else {
140 0 $delim = "\t";
141 }
142 3 ▶ while (<>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline ARGV)) 3 3 6
143 3 chomp;
144  
145 # Only waste time on lines with delimiters
146 3 ▶ if (/$delim/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/$delim/) { } 3 0 3
147 3 my @hunk = split /$delim/;
148 3 my $col = 0;
149 3 my @out;
150 3 foreach my $c (@cols) {
151 5 ▶ next if $c <= $col;
Branch: 1 branch
BranchTrueFalseTotal
if $c <= $col 0 5 5
152 5 push @out, $hunk[$c - 1];
153 5 $col = $c;
154 }
155 3 ▶ if ($to_end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to_end) 0 3 3
156 0 $col++;
157 0 foreach my $c ($col .. scalar(@hunk)) {
158 0 push @out, $hunk[$c - 1];
159 }
160 }
161 3 print join($delim, @out), "\n";
162 }
163 else { # no delimiter in line
164 0 ▶ print "$_\n" unless $sflag;
Branch: 1 branch
BranchTrueFalseTotal
unless $sflag 0 0 0
165 }
166 }
167 }
168  
169 0 ▶sub usage {
Subroutine usage: not called CC=1 SCAR=6.9
170 0 print <<EOT;
171 usage: $me -b list [-n] [file ...]
172 $me -c list [file ...]
173 $me -f list [-d delim] [-s] [file ...]
174  
175 Each LIST is made up of one range, or many ranges separated by commas.
176 Each range is one of:
177  
178 N Nth byte, character or field, counted from 1
179 N- from Nth byte, character or field, to end of line
180 N-M from Nth to Mth (included) byte, character or field
181 -M from first to Mth (included) byte, character or field
182  
183 EOT
184 0 exit EX_FAILURE;
185 }
186  
187 __END__
« bin/date ↑ Summary bin/nl »