Unix/Linux Go Back    

Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Grep Exact word

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-30-2013
tapia tapia is offline
Registered User
 
Join Date: Apr 2010
Last Activity: 10 February 2016, 9:44 PM EST
Posts: 24
Thanks: 9
Thanked 0 Times in 0 Posts
Grep Exact word

This may be stupid question but not able to solve it.

How to grep exact word and line along with it.

TEST:/u00/app/oracle/product/10.2.0/TEST:N
TEST2:/u00/app/oracle/product/10.2.0/ODS:N
TEST3:/u00/app/oracle/product/10.2.0/TEST:N
TEST4:/u00/app/oracle/product/10.2.0/ODS:N
TEST5:/u00/app/oracle/product/10.2.0/TEST:N
TEST6:/u00/app/oracle/product/10.2.0/ODS:N

if i do grep TEST /etc/oratab
it gives me all lines
How to grep lines only with TEST2.

is there any specific argument in grep.
    #2  
Old Unix and Linux 01-30-2013
srinivas matta srinivas matta is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 21 February 2013, 2:05 AM EST
Location: Hyderabad
Posts: 30
Thanks: 0
Thanked 8 Times in 8 Posts
you can google it also.


Code:
grep -w

The Following User Says Thank You to srinivas matta For This Useful Post:
tapia (01-30-2013)
    #3  
Old Unix and Linux 01-30-2013
itkamaraj's Unix or Linux Image
itkamaraj itkamaraj is offline Forum Advisor  
Registered User
 
Join Date: Apr 2010
Last Activity: 26 August 2016, 6:47 AM EDT
Posts: 3,082
Thanks: 37
Thanked 667 Times in 645 Posts
If you are interested in awk, then you can try the below.


Code:
 
$ awk -F: '$1=="TEST2"' input.txt
TEST2:/u00/app/oracle/product/10.2.0/ODS:N
 
$ awk -F: '$1=="TEST2" { print }' input.txt
TEST2:/u00/app/oracle/product/10.2.0/ODS:N

    #4  
Old Unix and Linux 01-30-2013
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 27 August 2016, 11:03 PM EDT
Location: NM
Posts: 10,805
Thanks: 439
Thanked 955 Times in 886 Posts

Code:
grep -F 'pattern'  filename

turns off regular expressions and does an exact text match instead.
    #5  
Old Unix and Linux 01-31-2013
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Last Activity: 27 August 2016, 7:41 PM EDT
Location: In the leftmost byte of /dev/kmem
Posts: 5,145
Thanks: 88
Thanked 1,323 Times in 991 Posts
Quote:
Originally Posted by tapia View Post
if i do grep TEST /etc/oratab
it gives me all lines
This is no wonder: when you search for "TEST", you will find everything containing "TEST", therefore "TEST2", "XTEST", "TESTX", etc.

If you want to find only specific lines you will have to enlarge your search pattern so much that it becomes distinct. In your case this could be done by including the ":", which seems to separate the first from the second part of the line. Searching for "TEST:" will only find the first line, not the others.

I hope this helps.

bakunin
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to grep the exact string / word ? adc22 UNIX for Dummies Questions & Answers 5 06-15-2012 10:39 AM
QUESTION1: grep only exact string. QUESTION2: find and replace only exact value with sed thibodc Shell Programming and Scripting 1 05-23-2012 11:14 PM
Find EXACT word in files, just the word: no prefix, no suffix, no 'similar', just the word chicchan UNIX for Dummies Questions & Answers 6 03-30-2012 04:28 PM
option of grep for counting exact word ?? maddy Linux 3 06-17-2008 08:55 AM
grep for "exact word" bullz26 Shell Programming and Scripting 7 03-14-2008 05:00 AM



All times are GMT -4. The time now is 01:09 AM.