LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 02-10-2010, 03:22 AM   #1
carolflb
Member
 
Registered: Oct 2009
Posts: 40

Rep: Reputation: 15
GREP: search special characters


Hi all,

I need to search for the following pattern with GREP in a text file:

word1->word2



So I tried already:

grep -n -m 1 -i -o -w -l "word1-[>]word2" <file
grep -n -m 1 -i -o -w -l "word1-\>word2" <file



But none of those works...I think probably because GREP doens't like the special character > in the middle of the serach pattern. Would somebody know how I can serach for it?

At the end I just need to now if GREP found the pattern in the file or not, so it should give me a 0 or a 1 back, once I check the value of the variable "?" after using the grep command.
 
Old 02-10-2010, 03:40 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984
Embedding in double quotes should work. What I don't get is why you used so many options if the requirement is just to test the presence of the string. The following should work:
Code:
grep -q "word1->word2" testfile && echo found it
where the && echo part is just for testing. Even the usage of a character list (square brackets in your first example) should work. If not you have to investigate a little more.
 
1 members found this post helpful.
Old 02-10-2010, 03:46 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Works OK for me (with zero or minimal options)
Code:
c:/tmp$ cat trash
word1->word2
word3
c:/tmp$ grep 'word1->word2' trash
word1->word2
c:/tmp$ grep -v 'word1->word2' trash
word3
 
1 members found this post helpful.
Old 02-10-2010, 04:06 AM   #4
carolflb
Member
 
Registered: Oct 2009
Posts: 40

Original Poster
Rep: Reputation: 15
Hi colucix, hi catkin,

thanks for the solutions, both of them work and the flag "-q" is exactly what I needed!
I think before I ended up lost with all those flags... I'm just a beginner and I don't know how to use all that very well..next time I will try to keep it simple.
 
Old 02-10-2010, 04:11 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984
You're welcome!
Quote:
Originally Posted by carolflb View Post
I think before I ended up lost with all those flags... I'm just a beginner and I don't know how to use all that very well..
He he.. that's one of the reasons why LinuxQuestions exists!
 
  


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
search and replace string having multiple special characters say_hi_ravi Linux - Newbie 4 08-26-2009 07:43 AM
search unknown special characters on a textfile bad_jaye Linux - Newbie 6 05-02-2008 09:55 PM
Special Characters SimeonV SUSE / openSUSE 14 07-07-2006 01:29 PM
special characters greenbox Linux - Software 9 12-23-2005 07:33 PM
grep problem in extracting special characters imppayel Programming 8 12-02-2004 07:28 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:19 PM.

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
Open Source Consulting | Domain Registration