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 10-25-2013, 05:29 PM   #1
123raajesh
LQ Newbie
 
Registered: Sep 2013
Posts: 12

Rep: Reputation: Disabled
linux grep multiple exact pattern not lines


Code:
Hi,

Need help to grep the following from a file x. I just want to grep exact match not lines and not partial word.

CONFSUCCESS
CONFFAIL
CONFPARTIALSUCCESS

>cat x
xczxczxczc zczczcxx CONFSUCCESS czczczcczc
czxxczxzxczcczc CONFFAIL xczxczcxcczczc
zczczczcz CONFPARTIALSUCCESS czczxcxzc

i tried this

grep -w "CONFSUCCESS\|CONFFAIL\|CONFPARTIALSUCCESS" x

 also this, getting only matching words not the whole word

grep -o "CONF*" x
CONF
CONFF
CONF

Want result as below

CONFSUCCESS
CONFFAIL
CONFPARTIALSUCCESS

Thanks
 
Old 10-25-2013, 05:39 PM   #2
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
Code:
grep -o "CONF.* " x

"CONF<anychar><previous 0 or more times><space>"

the assumption is your pattern is terminated by a space
 
Old 10-25-2013, 05:40 PM   #3
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 88
The pattern below failed because * in a regular expression means "zero or more of the preceding character":

Code:
grep -o "CONF*" x
Two ways of achieving your end are:

Code:
grep -o 'CONF[A-Z]*' x # match conf followed by zero or more upper-case letters
egrep -o 'CONFSUCCESS|CONFFAIL|CONFPARTIALSUCCESS' x # e(xtended)grep makes alternative patterns easier
 
3 members found this post helpful.
Old 10-25-2013, 05:43 PM   #4
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
Robhogg's
grep -o 'CONF[A-Z]*' x

is more 'exact' than mine
 
1 members found this post helpful.
  


Reply

Tags
grep


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 multiple lines that the pattern matches in bash niharikaananth Linux - Newbie 7 03-13-2012 08:39 PM
grep exact pattern kapila Linux - Newbie 11 08-06-2009 03:58 AM
how to match the exact pattern using grep utility vinaytp Linux - Newbie 3 05-11-2009 01:36 AM
grep lines between occurances of a pattern ksri07091983 Solaris / OpenSolaris 5 04-26-2009 07:14 AM
AWK/SED Multiple pattern matching over multiple lines issue GigerMalmensteen Programming 15 12-03-2006 06:08 PM


All times are GMT -5. The time now is 06:27 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration