LinuxQuestions.org
Visit Jeremy's Blog.
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 04-29-2009, 12:33 AM   #1
nagendrar
Member
 
Registered: Apr 2008
Location: HYD, INDIA.
Posts: 150

Rep: Reputation: 15
command for grep the exact word


I have two lines in a file like below

Host *
Host-specific *

Here I want to grep the "Host *" line only.

I had tried following way but I did not get exact line

grep -w Host filename


Please give the suggestions for grep exact line using specific word.


Thanks,
Nagendra.
 
Old 04-29-2009, 01:01 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,839

Rep: Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822
You already have it - it is simply that you have the wrong understanding of what a "word" is.
Have a look at the manpage again.

Simplest for what you want might be to use character classes - [[:space:]] say - before and after the "word" you want. May lead to other surprises of course ...
 
Old 04-29-2009, 02:38 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
a simple soln:
Code:
grep 'Host ' filename
 
Old 04-29-2009, 03:06 AM   #4
nagendrar
Member
 
Registered: Apr 2008
Location: HYD, INDIA.
Posts: 150

Original Poster
Rep: Reputation: 15
If there is tab space between Host and * then we did not get output using

grep 'Host ' filename.



Thanks,
Nagendra
 
Old 04-29-2009, 03:10 AM   #5
flopypl
LQ Newbie
 
Registered: Apr 2009
Location: Poland
Distribution: Slackware 12.1
Posts: 20

Rep: Reputation: 0
yesterday's thread: http://www.linuxquestions.org/questi...-linux-721931/

mayby that will help you something.
 
Old 04-29-2009, 04:12 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by nagendrar View Post
If there is tab space between Host and * then we did not get output using

grep 'Host ' filename.
Code:
grep Host[[:space:]] filename
the [:space:] class includes space, TAB, formfeed and so on.
 
Old 04-29-2009, 03:16 PM   #7
knudfl
LQ 5k Club
 
Registered: Jan 2008
Location: Copenhagen, DK
Distribution: pclos2016, Slack14.1 Deb Jessie, + 50+ other Linux OS, for test only.
Posts: 16,276

Rep: Reputation: 3154Reputation: 3154Reputation: 3154Reputation: 3154Reputation: 3154Reputation: 3154Reputation: 3154Reputation: 3154Reputation: 3154Reputation: 3154Reputation: 3154
How about

cat <file> | grep "\Host\ "
 
Old 04-29-2009, 03:33 PM   #8
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285
You may also want to try 'grep -x'

Code:
       -x, --line-regexp
              Select only those matches that exactly match the whole line.
Not sure if that's what you want to do tho.
 
  


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
grep question - match exact string Panagiotis_IOA Linux - General 2 01-20-2014 05:34 AM
grep the exact string only ZAMO Linux - General 11 08-28-2008 06:08 AM
grep exact letters packets Linux - Newbie 1 11-30-2007 09:39 PM
please read it i dont know the exact word to use to describe wat i wanna say pranith Slackware 5 05-30-2005 08:27 AM
grep [exact phrase] chrisfirestar Linux - General 2 02-09-2004 05:30 AM


All times are GMT -5. The time now is 12: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