Output regex match rather than result
brandonhaberfeld opened this issue · 0 comments
Neither grep nor sift can do what I describe below, but since sift is by far the better tool (use it absolutely everywhere), it would be better implemented here than grep:
We would like an option in sift that causes the rules used when a regex match is successful to be printed from the sift -f syntax, rather than the actual result (or both if someone needs that).
ie: file1 : contains a list of regex patterns to match against
file2 : contains the text to search or stdin if piped.
sift -f file_of-regex-patterns files-to-search-in
Wherever a file contains one of the regex patterns, return the pattern rather than the line in the files that matched (optionally)
Many people seem to want to do this (google search) and yet the only option is to iterate through all the lines is the searched text match one at a time to find which pattern worked.... awk is typically suggested but its regex engine is inferior.
Here an exmaple: The regexs to match some commands are:
File: patterns
.cmd1 (parm1|parm2|parm3)
^cmd2..sh (arg1|arg2)
Some text files contains various commands with options arguments and parameters:
file1: cmd1 parm2
file2: mycmd1 parm3
file3: cmd2-test-.sh arg2
file4: cmd2-test2.sh arg1
sift -f patterns file*
The output would be the lines of the pattern file that patched:
.cmd1 (parm1|parm2|parm3)
.cmd1 (parm1|parm2|parm3)
^cmd2..sh (arg1|arg2)
^cmd2..sh (arg1|arg2)