LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-09-2012, 06:20 AM   #1
gopinath
LQ Newbie
 
Registered: Sep 2007
Posts: 19

Rep: Reputation: 0
Need help regarding grep search


Hello Experts,

I need some help regarding searching a string using grep. The file contains strings similar to the below:

"google "."yahoo"
"google "."yahoo_test"

I want to search through the file for the keyword "google "."yahoo", when i search for yahoo like the below:

grep -iep "google "."yahoo" test.txt

I get nothing from the output. Please let me know how to work with these special characters.

WIth regards.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 03-09-2012, 06:33 AM   #2
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Palm Island
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,415

Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
cat test.txt | grep "google "."yahoo"
 
Old 03-09-2012, 10:14 AM   #3
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
Are the quotes, the whitespace, or the dot significant to you? If not:
Code:
$ grep 'google.*yahoo' test.txt
If so, please explain your data and goal more thoroughly.
 
Old 03-10-2012, 07:12 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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:

Code:
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).

Code:
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:

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes



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.

Code:
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.

Code:
grep -iep ...
-p 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.

Also, -e is there for supplying an expression to match, and the argument that immediately follows it must be that expression.

Code:
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 "-".
 
3 members found this post helpful.
Old 03-12-2012, 01:33 AM   #5
gopinath
LQ Newbie
 
Registered: Sep 2007
Posts: 19

Original Poster
Rep: Reputation: 0
Hello experts,

Thanks for the inputs. The idea is to grep the word "google "."yahoo" as a single word, I am working on a script on AIX, where I try to extract the table definitions from a file where the table names will be of format "schema "."tablename" so AIX has an option of -p which will enable us to extract the whole paragraph which contains the word. I used many unwanted ways to grep the string. But finally got the simple solutions from you experts.

Thanks a lot.

With regards,
Gopinath.
 
Old 03-12-2012, 01:57 AM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by Satyaveer Arya View Post
cat test.txt | grep "google "."yahoo"
And this is really poor advice; not only does it not help with his actual
problem at hand, it also advocates pointless use of cat ("If you find
yourself using 'cat' with only one file as parameter you're most likely
using it wrong").



Cheers,
Tink
 
Old 03-12-2012, 02:27 AM   #7
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Palm Island
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,415

Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
Quote:
And this is really poor advice; not only does it not help with his actual
problem at hand, it also advocates pointless use of cat ("If you find
yourself using 'cat' with only one file as parameter you're most likely
using it wrong").
Ohhh god, that's really the poor advice. I made that a blunder mistake
 
Old 03-12-2012, 02:42 AM   #8
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Palm Island
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,415

Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
Quote:
cat test.txt | grep "google "."yahoo"
gopinath, I'm sorry for that suggestion. I don't understand how I suggested you that, a blunder mistake.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
grep search help sublime188 Linux - Newbie 10 11-29-2011 04:18 PM
search with grep andy.l Linux - General 1 05-16-2011 02:59 AM
[SOLVED] grep search between n-m X.Cyclop Programming 4 10-06-2010 05:28 AM
grep search... tobiasw Linux - General 4 10-28-2006 02:36 PM
Using Grep to search all sub-directories yrraja Solaris / OpenSolaris 2 08-28-2004 04:29 AM


All times are GMT -5. The time now is 10:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration