Make your own Grep with Perl (Script)

By | Perl Scripts

Grep is cool but it does not use PCRE (BOO!) so make your own! You can save the code as anything you like (mygrep.pl for example.)

In the code snippet below it assumes the first argument will be a regex and anything else will be processed as a file. If it comes in from STDIN (left side) it will parse the literal file names. If it comes in as an argument (right side) it will open the file and compare the regex line by line.

#!/usr/bin/perl -nl
BEGIN
{
    unless (@ARGV == 1)
    {
        print qq~Need a Regex Pattern!\n~;
        exit 69;    
    }
    $regex = shift @ARGV;
}
print $_ if m~${regex}~;

USE CASE 1: mygrep.pl [REGEX PATTERN] [FILES]

USE CASE 2: [STDOUT] | mygrep.pl [REGEX PATTERN]

One Response to " Make your own Grep with Perl (Script) "

  1. […] Command Line Weather One-Liner Renaming Files with PCRE via the Command Line Make your own Grep with Perl (Script) https://geekalicious.blog/category/techy/wget/ Disclaimer […]

Leave a Reply

Your email address will not be published. Required fields are marked *