This repository has been archived by the owner on Aug 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcutb.pl
105 lines (91 loc) · 2.06 KB
/
cutb.pl
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
#!/usr/bin/perl
use strict;
use warnings;
use open IO => q{:bytes};
# name........: cutb.pl
# author......: Rub3nCT
# based on....: cutb.c by atom
# description.: cut the specific prefix or suffix length off STDIN and pass it to STDOUT.
# added.......: only prints uniq words to STDOUT (-u / --uniq).
sub usage
{
print "\n usage: $0 [-u] offset [length] < infile > outfile\n";
}
if (($#ARGV < 0) || ($#ARGV > 2)) {
usage ();
exit 1;
}
my ($uniq, $offset, $length) = (0, 0, 0);
if ($#ARGV == 0) {
if (($ARGV[0] eq '-u') || ($ARGV[0] eq '--uniq')) {
usage ();
die "\n Error: an offset value must be specified!!!\n";
}
else {
$offset = $ARGV[0];
}
}
if ($#ARGV == 1) {
if (($ARGV[0] eq '-u') || ($ARGV[0] eq '--uniq')) {
$uniq = 1;
$offset = $ARGV[1];
}
else {
$offset = $ARGV[0];
$length = $ARGV[1];
}
}
if ($#ARGV == 2) {
if (($ARGV[0] eq '-u') || ($ARGV[0] eq '--uniq')) {
$uniq = 1;
$offset = $ARGV[1];
$length = $ARGV[2];
}
else {
usage ();
die "\n Error: invalid arguments specified!!!\n";
}
}
if ($offset !~ /^[-]?\d+$/) {
usage ();
die "\n Error: offset value must be an integer!!!\n";
}
if ($length !~ /^[-]?\d+$/) {
usage ();
die "\n Error: length value must be an integer!!!\n";
}
if ($uniq == 1) {
my %uniq;
while (<STDIN>) {
tr/\r\n$/\n/d;
chomp;
next if $_ eq q{};
my $str = $_;
if ($length != 0) {
$str = substr $_, $offset, $length;
}
else {
$str = substr $_, $offset;
}
next if $str eq q{};
$uniq{$str}++;
next if $uniq{$str} > 1;
print "$str\n";
}
}
else {
while (<STDIN>) {
tr/\r\n$/\n/d;
chomp;
next if $_ eq q{};
my $str = $_;
if ($length != 0) {
$str = substr $_, $offset, $length;
}
else {
$str = substr $_, $offset;
}
next if $str eq q{};
print "$str\n";
}
}