Skip to content

Commit

Permalink
Merge pull request #299 from mknos/cut-field0
Browse files Browse the repository at this point in the history
cut: -b 0 and -f 0 are invalid
  • Loading branch information
briandfoy authored Oct 23, 2023
2 parents d5eac03 + 60f31f8 commit 02e726f
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions bin/cut
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,22 @@ $opt{b} = $opt{c} if defined $opt{c};
if (defined ($opt{b})) {

my @list = split (/,/, $opt{b});
foreach my $item (@list) {
if ($item == 0) {
warn "$me: byte positions are numbered from 1\n";
exit EX_FAILURE;
}
}

while (<>) {
chomp;

foreach my $item (@list) {
my ($start,$end) = split (/-/, $item);
die "$me: invalid byte list\nType '$me' alone for usage.\n"
if ($start and $end and $start > $end); # parameters overlap?
if ($start and $end and $start > $end) {
warn "$me: invalid byte list\n";
exit EX_FAILURE;
}

# change cut's list parameters to substr's parameters
$start--; # cut counts from 1, not 0
Expand All @@ -92,6 +100,12 @@ if (defined ($opt{b})) {
elsif (defined ($opt{f})) {

my @list = split (/,/, $opt{f});
foreach my $item (@list) {
if ($item == 0) {
warn "$me: fields are numbered from 1\n";
exit EX_FAILURE;
}
}

my $delim = "\t";
$delim = substr ($opt{d}, 0, 1) if defined $opt{d};
Expand All @@ -103,8 +117,10 @@ elsif (defined ($opt{f})) {
if (/$delim/) {
foreach my $item (@list) {
my ($start,$end) = split (/-/, $item);
die "$me: invalid byte list\nType '$me' alone for usage.\n"
if ($start and $end and $start > $end); # parameters overlap?
if ($start and $end and $start > $end) {
warn "$me: invalid field list\n";
exit EX_FAILURE;
}

# change cut's list parameters to substr's parameters
$start--; # cut counts from 1, not 0
Expand Down

0 comments on commit 02e726f

Please sign in to comment.