grep search by mysql templates
I'm trying search in filenmae by grep, but I have a some problem..
I want search by templates
'apple banana'
Find rows that contain at least one of the two words.
'+apple +juice'
Find rows that contain both words.
'+apple macintosh'
Find rows that contain the word “apple”, but rank rows higher if they also contain “macintosh”.
'+apple -macintosh'
Find rows that contain the word “apple” but not “macintosh”.
'+apple ~macintosh'
Find rows that contain the word “apple”, but if the row also contains the word “macintosh”, rate it lower than if row does not. This is “softer” than a search for '+apple -macintosh', for which the presence of “macintosh” causes the row no开发者_JAVA百科t to be returned at all.
'+apple +(>turnover <strudel)'
Find rows that contain the words “apple” and “turnover”, or “apple” and “strudel” (in any order), but rank “apple turnover” higher than “apple strudel”.
'apple*'
Find rows that contain words such as “apple”, “apples”, “applesauce”, or “applet”.
'"some words"'
Find rows that contain the exact phrase “some words” (for example, rows that contain “some words of wisdom” but not “some noise words”). Note that the “"” characters that enclose the phrase are operator characters that delimit the phrase. They are not the quotation marks that enclose the search string itself.
First rule: grep "apple\|banana" filename
Other rules are causing problems. Help me pls, sorry for bad english.
You can't do ratings with grep so those examples will require further logic. The other examples can be solved with or
operator and the ?, *, and + qualifiers. Here a couple of examples of the regular expressions you need:
wesbailey@feynman:~/tmp> cat file.txt
apple strudel
apple other txt
apple macintosh other text
macintosh other txt
other text strudel
Fnd rows that contain at least one of the two words:
wesbailey@feynman:~/tmp> grep -E 'apple|strudel' file.txt
apple strudel
apple other txt
apple macintosh other text
other text strudel
Find rows that contain both words:
wesbailey@feynman:~/tmp> grep -E '(apple.*macintosh)|(macintosh.*apple)' file.txt
apple macintosh other text
Find rows that contain the word “apple” but not “macintosh”:
wesbailey@feynman:~/tmp> grep -E 'apple' file.txt | grep -v 'macintosh'
apple strudel
apple other txt
Find rows that contain the exact phrase “some words”
grep '"some words"' file.txt
Hope this helps
精彩评论