-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathocut
More file actions
executable file
·82 lines (58 loc) · 1.98 KB
/
ocut
File metadata and controls
executable file
·82 lines (58 loc) · 1.98 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
#!/usr/bin/env perl
#
# ocut - ordered cut, change the order of selected columns
#
# written by Phillip Dexheimer (pdexheimer@gmail.com), 2019
use Getopt::Long;
use Pod::Usage;
# Standard module in perl - the parse_line function handles quoted delimiters
use Text::ParseWords;
my $delim = "\t";
my @fields = ();
my $field_str = "";
my $only_delimited = 0;
my $help = '';
GetOptions("delim=s" => \$delim,
"fields=s" => \$field_str,
"only-delimited|s" => \$only_delimited,
"help|?" => \$help) or pod2usage(2);
pod2usage(-verbose => 1, -exitval => 1) if ($help);
@fields = map { $_ - 1 } map {
if (/(\d+)-(\d+)/) {
$1 .. $2;
} else {
$_;
}
} split /,/, $field_str;
while (<>) {
chomp;
my @F = parse_line($delim, 0, $_);
if (@F == 1 && !$only_delimited) {
print "$F[0]\n";
continue;
}
my @output = ();
push (@output, $F[$_]) foreach (@fields);
print join($delim, @output), "\n";
}
__END__
=head1 NAME
ocut - select parts of a file, in a specified order
=head1 SYNOPSIS
ocut [OPTIONS] [FILE]
Options:
-d, --delim DELIM Specifies the column delimiter (default <TAB>)
-f, --fields FIELDS Which columns of the input should be selected?
-s, --only-delimited If specified, lines without a delimeter are suppressed
If FILE is not specified on the command line (or is -), standard input will be read.
This is similar to cut, except that output fields will be in the specified order
=head1 OPTIONS
=over 4
=item B<--delim>
The delimiter may be present within a column if it is backslash-escaped (ie, \,)
=item B<--fields>
The (1-based) index of the columns to output. As with cut, fields may be comma-separated and contiguous ranges may be specified. The order of fields given to this parameter will be the order of fields in the output.
=item B<--only-delimited>
As in cut, by default lines that do not contain a delimiter are passed through to the output. Specify this option to suppress them.
=back
=cut