Skip to content

Instantly share code, notes, and snippets.

@farooqkz
Last active December 13, 2017 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save farooqkz/29497f70524fba73a451345859d395bc to your computer and use it in GitHub Desktop.
Save farooqkz/29497f70524fba73a451345859d395bc to your computer and use it in GitHub Desktop.
More advanced look utility
#!/usr/bin/perl -Tw
##############################################################################
#
# Copyright (C) 2017 Farooq Karimi Zadeh <farooghkarimizadeh@gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
##############################################################################
$dict = "/usr/share/dict/words";
$argc = @ARGV;
if ($argc == 1) {
if ($ARGV[0] eq "-h" || $ARGV[0] eq "--help"){ #
print "usage: look.pl <pattern> [file]\n\tBy default <file> is /usr/share/dict/words\n";
exit 0;
}else{
$pattern = $ARGV[0];
}
}elsif ($argc == 2){
$pattern = $ARGV[0];
$dict = $ARGV[1];
}else{
print "Wrong argument count: $argc\nSee look.pl -h\n";
exit 1;
}
open $fp, $dict or die "Cannot open file: $dict";
while (<$fp>){
if (/$pattern/){
print;
}
}
close $fp or die "Cannot close file: $dict";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment