LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 01-18-2018, 12:36 PM   #1
machika
LQ Newbie
 
Registered: Jan 2018
Posts: 11

Rep: Reputation: Disabled
grep word finder?


Hi,
Can't find the answer to this googlewise.

When trying to find all <the> in file.txt using,

Quote:
$ grep the file.txt
it shows,

Quote:
for the time neither or further
How can you make it show <the> exclusively and ignore, neither & further?
 
Old 01-18-2018, 12:38 PM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Yes, what you are looking for are the word boundaries. The different regular expression styles use different notation to represent them. [:<:] and [:>:], or \< and \>, or \b

They need to be before and after your pattern.
 
Old 01-18-2018, 04:44 PM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,125

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
grep has parameters for just this task. See the manpage.
Of course word-bounding also works.

Both have been known to cause surprises.
 
Old 01-18-2018, 05:00 PM   #4
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
I usually forget how to get whole words, so I just add spaces before and after.
 
Old 01-19-2018, 01:28 AM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Quote:
Originally Posted by AwesomeMachine View Post
I usually forget how to get whole words, so I just add spaces before and after.
That works only for words in the middle of a sentence without punctuation. Look into the word boundary markers for more accurate results which will include words near punctuation or at the beginning or end of a line.
 
Old 01-19-2018, 03:35 AM   #6
JJJCR
Senior Member
 
Registered: Apr 2010
Posts: 2,150

Rep: Reputation: 449Reputation: 449Reputation: 449Reputation: 449Reputation: 449
check out link below:
https://unix.stackexchange.com/quest...ord-boundaries

Never test but I think this might work:

grep ' the ' < file.txt

Last edited by JJJCR; 01-19-2018 at 03:35 AM. Reason: edit
 
Old 01-19-2018, 03:48 AM   #7
machika
LQ Newbie
 
Registered: Jan 2018
Posts: 11

Original Poster
Rep: Reputation: Disabled
@JJJCR

Quote:
Never test but I think this might work:

Quote:
grep ' the ' < file.txt
You nailed it. Thanks.
 
Old 01-19-2018, 03:55 AM   #8
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,125

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Expect to be surprised at some time in the future. Read post #5.
 
Old 01-21-2018, 07:15 PM   #9
JJJCR
Senior Member
 
Registered: Apr 2010
Posts: 2,150

Rep: Reputation: 449Reputation: 449Reputation: 449Reputation: 449Reputation: 449
Lightbulb

Quote:
Originally Posted by machika View Post
@JJJCR



You nailed it. Thanks.
Thanks for the update, glad to know it works! Cheers!
 
  


Reply



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
Filter word from grep kkrrss Linux - Server 7 02-23-2017 05:36 PM
[SOLVED] grep for word cbtshare Linux - Newbie 8 09-04-2015 08:38 AM
grep exact word - to find file which start with some word publicLearner Linux - Newbie 8 07-23-2015 03:12 AM
grep a single word...not two! Drigo Linux - Newbie 6 06-26-2013 12:34 PM
grep data between two word DropSig Linux - General 2 01-27-2006 08:31 PM

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

All times are GMT -5. The time now is 02:53 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
Open Source Consulting | Domain Registration