Using Grep with Pattern File and PCRE
I would like to write a newline delimeted rules file using PCREs for use with the grep command. Grep has the option -f to obtain the search pattern from a file, and option -P to search using PCREs. However, these two options do not work together. The -f option only seems to work with fixed string rules.
A friend previously helped me get around this limitation somehow, but I can't remember how he did it. I also would like the ability to add comments at the end of each rule in the file. Example Rules File- Code:
'^John.*Sally$' # 09/22/10 - Per Steve Johnson Code:
cat data.file | grep -P -f rules.file # Doesn't work |
I'm not getting what yer tryin to do.
One thing (and I know this is not addressing your question), you can lose the 'cat' part before the pipe and just run: grep -P -f rules.file data.file Per the grep man page, -P is experimental.... |
Quote:
I don't know how to do this with grep. This is possible with awk and perl scripting, but that's probably more time consuming than the poster wants. One alternative might be, since you're using regular expressions, to wrap the whole thing in a series of OR groups. So for "Hello" and "World" you could use "(Hello|World)" |
The '-e' switch allows for multiple expressions so you can do a 'grep -e <item> -e <item>' over and over again....can also do the multiple grep's seperated by pipes but that is so lame.
I used awk to find any lines with IMAGE and print column 6 and find any lines with FRAG and print column 9: awk '{ if($1=="IMAGE") print $6; if($1=="FRAG") print $9}' Not sure if any of this is helpful. |
You could iterate over all patterns in your file, and run grep with individual patterns.
Code:
while read pattern; do |
You could look at egrep for more advanced options.
|
Thank you for the responses and that's an interesting solution theNbomr. Unfortunately my Linux computer is not working so well right now so I am forced to use the Windows machine to post. However, I did get in touch with my friend and he reminded me about how we did this before. This isn't going to be 100% correct but I will edit it later when I am able to test it.
Code:
grep -P '`cat rules.file | sed -r 's/ *\#.*//' | tr '\n' '\|' | sed -r 's/\|$//'`' data.file Grep *should* interpret this command as: Code:
grep -P 'rule1|rule2|rule3' data.file |
So ultimately it appears you could replace all of:
Code:
grep -P '`cat rules.file | sed -r 's/ *\#.*//' | tr '\n' '\|' | sed -r 's/\|$//'`' data.file Code:
grep -P $(awk -F="[ \t]*#" '{print $1}' rules.file) data.file |
Quote:
A better way is to concat the regex pattern first, then pass the pattern to grep instead of calling grep for every pattern iterated. Code:
grep -E $(sed 's/[ \t]*#.*//' rules |tr "\n" "|"|sed 's/|$//') file |
Thank you for the responses and the awesome awk solution! Here is what ended up working (with the awk command as well):
Code:
grep -P "`cat rules.file | grep -vP '^$' | sed -r 's/\s*\#.*//' | tr '\n' '\|' | sed -r 's/\|$//'`" data.file Also added the grep command to remove blank lines. |
Well I am glad you have a solution although I am not sure why you need so many calls to all the different apps. Calling cat is definitely not required
|
I agree and I'm just not familiar with awk yet. I will use your example to see if I can use that instead.
|
All times are GMT -5. The time now is 04:46 AM. |