Please use [code][/code]
tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.
As asked before, are the quotation marks a literal part of the string you want to match? I'm going to assume so for the rest of the post.
Remember, quotes have syntactical meaning to the shell
; they group strings together and escape other characters that have special meaning. They are generally removed when the shell parses the line. So this command:
grep -i "google "."yahoo" test.txt
Actually means grep searches for the string: [google .yahoo
(More pedantically, the shell removes the quotes from and concatenates three separate strings, "google
", and "yahoo
" into a single final expression, which is then passed to grep
To include literal double-quotes and other shell-reserved characters, you have to escape them in some way. One possibility is to enclose the whole string inside a set of single quotes (singles are escaped by doubles, and vice-versa).
grep -i '"google "."yahoo"' test.txt
Shell quoting and argument processing is a fairly complex, but vitally important, topic, so I suggest you do some reading. Start with these links:
Second, the "." period is considered a special character in regular expressions
, which grep assumes the expression to be, by default. It means "match any single character". To ensure that grep searches for a literal period, you (again) have to escape it, only this time in regex syntax terms.
The proper way to handle such characters is generally to enclose them in a bracket expression, although a backslash will also generally work. You can also use the -F
(fixed string) option to disable regex processing entirely.
grep -i '"google "[.]"yahoo"' test.txt
grep -i '"google "\."yahoo"' test.txt
grep -iF '"google "."yahoo"' test.txt
Finally, a few more comments regarding the grep options you used above.
is, AFAICT, not a supported grep option. At least not for gnu
grep. Unless you mean -P
(use perl regex) instead? But perl regex isn't necessary for a simple string match like this.
is there for supplying an expression to match, and the argument that immediately
follows it must be that expression.
grep -ieo "searchstring" file #no good ("o" will be considered the expression,
#and "searchstring" a filename.)
grep -ioe "searchstring" file #good
grep -io -e "searchstring" file #good
grep -e "searchstring" -io file #good
Note also that the use of -e
is optional when you're supplying only a single expression. You only need to use it when giving multiple expressions at once, or perhaps if the expression itself starts with a "-