-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringy
More file actions
executable file
·69 lines (58 loc) · 1.71 KB
/
stringy
File metadata and controls
executable file
·69 lines (58 loc) · 1.71 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
#!/usr/bin/perl -w
use warnings;
use strict;
my $argc = $#ARGV+1;
if ($argc == 0) {
show_missing();
} elsif ($argc <= 2) {
show_missing();
} elsif ($argc == 3) {
if (($ARGV[0] eq '-s') or ($ARGV[0] eq '--search')) {
search($ARGV[1], $ARGV[2]);
} else {
show_missing();
}
} elsif ($argc == 4) {
if (($ARGV[0] eq '-r') or ($ARGV[0] eq '--replace')) {
replace($ARGV[1]);
} else {
show_missing();
}
}
sub show_help {
print "Usage: stringy (OPTION) [FILE] [ITEMS]\n"
. "Perform simple operations on a given file.\n"
. "\n"
. "-h, --help\t" . "Display this help and exit.\n"
. "-s, --search\t" . "Searches the second argument for the first.\n"
. "-r, --replace\t" . "Replace the first argument with the second, inside the given third argument.\n"
. "-rm, --remove\t" . "Remove any instances of the first argument in the given second.\n"
}
sub show_missing {
print "stringy: missing operand(s)\n"
. "Try 'stringy --help' for more information.\n";
}
sub search {
my $fname = shift;
my $item = shift;
my $line_count = 0;
my $found = 0;
my $col_count;
open (my $fhandle, '<', $fname) or die "Could not open '$fname' for read: $!";
for (<$fhandle>) {
$line_count++;
unless (($col_count = index($_, $item)) == -1) {
$col_count++; # increment line count to substitute for 0 position indexing
print "'$item' found at line $line_count, column $col_count.\n";
$found = 1;
}
}
print "No occurence of '$item' was found in '$fname'.\n" if $found == 0;
close $fhandle;
}
sub replace {
# body...
}
sub remove {
# body...
}