Egrep with exact words


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Egrep with exact words
# 1  
Old 12-13-2012
Egrep with exact words

Hi, I have the following data in a file
Code:
bob
bobby
sam
sammy
ed
eddie

I need to grep these exact words: "bob", "sam", "ed". I don't want to see "bobby", "sammy", "eddie".

Code:
egrep 'bob|sam|ed'

gives me everything because egrep interprets these as a pattern. How can I specify an exact word?

Thanks everyone.

Last edited by Franklin52; 12-14-2012 at 03:24 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 12-14-2012
Use -w option:-
Code:
egrep -w "bob|sam|ed" filename

# 3  
Old 12-14-2012
also, try using double quotes instead of single quotes for search expression. It can make a difference depending on OS, UX flavor.
# 4  
Old 12-14-2012
Note that the -w option is not in the standards and is not present on all systems. And since you have one word per line in your input file it is easy to match what you want even when the -w option is not available. The standards also say that egrep is obsolete and need not be available on conforming implementations. If you're writing new code, I'd suggest either:
Code:
grep -E '^(bob|sam|ed)$' file

or
Code:
grep -E '^bob$|^sam$|^ed$' file

even though I don't know of any implementation that doesn't still treat egrep as a synonym for grep -E.

As far as using single quotes or double quotes goes; either will produce the same results for these words. I use single quotes here because there is no need for the shell to perform the extra expansions that double quotes require; so single quotes are slightly faster.
# 5  
Old 12-14-2012
Or:
Code:
grep -xE 'bob|sam|ed' file


--
To make it insensitive to extra white space:
Code:
awk '$1~/^(bob|sam|ed)$/' file

Code:
grep -E '^[[:space:]]*(bob|sam|ed)[[:space:]]*$' file


Last edited by Scrutinizer; 12-14-2012 at 04:05 AM..
# 6  
Old 12-18-2012
Thanks for all your comments. The use of '^' and '$' work only if the keyword is the only word in the sentence. What if the keywords appear in the middle of a sentence? For example,

Code:
 
monday bob plays football
tuesday bobby plays hockey
wednesday sam plays volleyball
thursday sammy plays basketball
friday ed plays baseball
saturday eddie plays soccer

What grep command only gives me 'bob, sam, ed'?
# 7  
Old 12-18-2012
The following will give you lines containing bob, sam, or ed on a line by themselves, at the start of a line followed by a space, at the end of a line preceded by a space, or in the middle of a line with a space before and after the word:
Code:
grep -E '(^(bob|sam|ed)$)|(^(bob|sam|ed) )|( (bob|sam|ed) )|( (bob|sam|ed)$)' file

This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace particular words in file based on if finds another words in that line

Hi All, I need one help to replace particular words in file based on if finds another words in that file . i.e. my self is peter@king. i am staying at north sydney. we all are peter@king. How to replace peter to sham if it finds @king in any line of that file. Please help me... (8 Replies)
Discussion started by: Rajib Podder
8 Replies

2. UNIX for Beginners Questions & Answers

Count exact matched words

hi , i have a file test.dat which contains following data. test.dat XY|abc@xyz.com XY|abc@xyz.com ST|abc@xyz.com ST|abc@xyz.com ST|XYZ@abc.com FK|abc@xyz.com FK|STG@xyz.com FK|abc@xyz.com FK|FKG@xyz.com i want to know the count of XY,ST,FK. i.e XY = 2 , ST = 3 , FK = 4 I am... (4 Replies)
Discussion started by: itzkashi
4 Replies

3. Shell Programming and Scripting

Gawk gensub, match capital words and lowercase words

Hi I have strings like these : Vengeance mitt Men Vengeance gloves Women Quatro Windstopper Etip gloves Quatro Windstopper Etip gloves Girls Thermobite hooded jacket Thermobite Triclimate snow jacket Boys Thermobite Triclimate snow jacket and I would like to get the lower case words at... (2 Replies)
Discussion started by: louisJ
2 Replies

4. Shell Programming and Scripting

How count the number of two words associated with the two words occurring in the file?

Hi , I need to count the number of errors associated with the two words occurring in the file. It's about counting the occurrences of the word "error" for where is the word "index.js". As such the command should look like. Please kindly help. I was trying: grep "error" log.txt | wc -l (1 Reply)
Discussion started by: jmarx
1 Replies

5. Shell Programming and Scripting

Grep two words with exact match

HI Input : Counters Counter Int Ints Counters Counters Ints Ints I want to grep Counter|Int Output : Counter (1 Reply)
Discussion started by: pareshkp
1 Replies

6. Shell Programming and Scripting

echo exact xml tag from an exact file

Im stumped on this one. Id like to echo into a .txt file all names for an xml feed in a huge folder. Can that be done?? Id need to echo <name>This name</name> in client.xml files. $path="/mnt/windows/path" echo 'recording names' cd "$path" for names in $path than Im stuck on... (2 Replies)
Discussion started by: graphicsman
2 Replies

7. Shell Programming and Scripting

QUESTION1: grep only exact string. QUESTION2: find and replace only exact value with sed

QUESTION1: How do you grep only an exact string. I am using Solaris10 and do not have any GNU products installed. Contents of car.txt CAR1_KEY0 CAR1_KEY1 CAR2_KEY0 CAR2_KEY1 CAR1_KEY10 CURRENT COMMAND LINE: WHERE VARIABLE CAR_NUMBER=1 AND KEY_NUMBER=1 grep... (1 Reply)
Discussion started by: thibodc
1 Replies

8. Shell Programming and Scripting

Shell script to find out words, replace them and count words

hello, i 'd like your help about a bash script which: 1. finds inside the html file (it is attached with my post) the code number of the Latest Stable Kernel, 2.finds the link which leads to the download location of the Latest Stable Kernel version, (the right link should lead to the file... (3 Replies)
Discussion started by: alex83
3 Replies

9. UNIX for Dummies Questions & Answers

search ")" with egrep - egrep: syntax error

Hi Guys, we have a shell script which basically query the Database which retrieves huge data and use the data with "egrep" . Now there is some data which contains characters like "abc)" and the same is used like below : "egrep (.+\|GDPRAB16\|GDPR/11702 96 abc)\|$ temp.txt" now while... (7 Replies)
Discussion started by: sagarjani
7 Replies

10. UNIX for Dummies Questions & Answers

Egrep cheat sheet anywhere? Looking for meaning of egrep -c

Hi I've been searching google and have not found what egrep -c means. Does anyone know where I can get a cheat sheet or what that -c means? thanks, Linda (2 Replies)
Discussion started by: leelm
2 Replies
Login or Register to Ask a Question