-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathcolrm
executable file
·123 lines (89 loc) · 2.09 KB
/
colrm
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
#!/usr/bin/perl
=begin metadata
Name: colrm
Description: remove columns from a file
Author: Jeffrey S. Haemer
License: perl
=end metadata
=cut
use strict;
use File::Basename qw(basename);
use constant EX_SUCCESS => 0;
use constant EX_FAILURE => 1;
my $Program = basename($0);
if (@ARGV > 2) {
warn "usage: $Program [startcol [endcol]]\n";
exit EX_FAILURE;
} elsif (@ARGV == 0) {
print while(<>);
} elsif (@ARGV == 1) {
my $startcol = getarg();
while (my $line = <>) {
chomp $line;
my $len = length $line;
if ($startcol > $len) {
print $line;
} else {
print substr $line, 0, $startcol - 1;
}
print "\n";
}
} elsif (@ARGV == 2) {
my $startcol = getarg();
my $endcol = getarg();
if ($startcol > $endcol) {
warn "$Program: bad range: $startcol,$endcol\n";
exit EX_FAILURE;
}
while (my $line = <>) {
chomp $line;
my $len = length $line;
if ($startcol > $len) {
print $line;
} else {
print substr $line, 0, $startcol - 1;
print substr $line, $endcol;
}
print "\n";
}
}
exit EX_SUCCESS;
sub getarg {
my $n = shift @ARGV;
if (!defined($n)) {
warn "$Program: missing argument\n";
exit EX_FAILURE;
}
if ($n =~ m/[^0-9]/ || $n == 0) {
warn "$Program: invalid column number '$n'\n";
exit EX_FAILURE;
}
return $n;
}
=head1 NAME
colrm - remove columns from a file
=head1 SYNOPSIS
colrm [startcol [endcol]]
=head1 DESCRIPTION
B<colrm> removes the named columns from each line of its standard input
(one column = one character).
Column numbering starts at 1, not 0.
If a only I<startcol> is provided, removes all columns from I<startcol>
rightwards.
If both I<startcol> and I<endcol> are provided, removes all columns from
I<startcol> to I<endcol>, inclusive.
If neither is provided, acts just like B<cat>.
=head1 OPTIONS AND ARGUMENTS
=over 2
=item I<startcol>
The first column to remove.
=item I<endcol>
The last column to remove.
=back
=head1 AUTHOR
Jeffrey S. Haemer
=head1 BUGS
Lacks the special-case handling of backspace and tab added in some
other versions. Acts, instead, like the simpler Linux and SunOS versions.
=head1 SEE ALSO
awk(1)