mgrep - Multiple Regex Grep - SotD
My newest Ruby utility is called mgrep. It is a multiple regular expression version of grep. Input is processed line by line and each regular expression is said by the user to match or not match. All lines that meet the user’s desires for matching/non-matching regular expressions are printed in the following format: “filename [line number]: line of text.”
This can be taken in a number of different directions. I’m thinking “Partial Matches” meaning one regular expression matches one line in the file and eventually a second regular expression matches a totally different line and if all of these conditions succeed by the end of the file then print out the filename. This sounds more useful and will most likely be in version 1.0.
Here is the current usage:
usage: mgrep [-#] ( [-n] regex ) [filenames]
# - the number of regular expressions, defaults to 1
( ... ) - there should be # of these
regex - regular expessions to be checked on the line
filenames - names of the input files to be parsed, if blank uses STDIN
options:
--neg, --not, or -n line must not match this regular expression
special note:
When using bash, if you want backslashs in the replace portion make sure
to use the multiple argument usage with single quotes for the replacement.
The usage is a little confusing seeing as the number of regular expressions on the command line are variable based on another command line switch. All in all though it is rather clear. Options will likely come in the future, much like grep or awk if I get around to it.
Here is an example probably not useful but at least it shows functionality, the line must contain a number, a double letter, and end with a !: [input]
1 ab !
- aa !
1 aa -
1 aa !
oh yah! 1
And when I run my script, I’ll put all the regular expressions in /here/ to make it clearer, this syntax is allowed by mgrep for convenience. Here is what it looks like:
joe[~/sandbox]$ mgrep -3 ‘/\\d/’ ‘/(\\w)\\1/’ ‘/!$/’ input
input [4]: 1 aa !
To show of the –neg or -n option this command will show all the lines that do not have a hypen and still end with a !:
joe[~/sandbox]$ mgrep -2 -n ‘/-/’ ‘/!$/’ input
input [1]: 1 ab !
input [4]: 1 aa !
The script surely be updated soon, but grab it now and try it out:
mgrep - Most Recent Version - Download
mgrep - changelog
