LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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
 
LinkBack Search this Thread
Old 04-28-2009, 11:33 PM   #1
nagendrar
Member
 
Registered: Apr 2008
Location: HYD, INDIA.
Posts: 107

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, 12:01 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 10,456

Rep: Reputation: 622Reputation: 622Reputation: 622Reputation: 622Reputation: 622Reputation: 622
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, 01:38 AM   #3
chrism01
Guru
 
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.2, Centos 5.8
Posts: 11,740

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

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, 02: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, 03:12 AM   #6
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: OpenSUSE 12.1 CentOS 6.2
Posts: 8,998

Rep: Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349
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, 02:16 PM   #7
knudfl
LQ 5k Club
 
Registered: Jan 2008
Location: Copenhagen, DK
Distribution: pclos2010.12, Slack1337 DebSqueeze, +50+ other Linux OS, for test only.
Posts: 9,293

Rep: Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350
How about

cat <file> | grep "\Host\ "
 
Old 04-29-2009, 02:33 PM   #8
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Posts: 11,388
Blog Entries: 2

Rep: Reputation: 825Reputation: 825Reputation: 825Reputation: 825Reputation: 825Reputation: 825Reputation: 825
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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
grep the exact string only ZAMO Linux - General 11 08-28-2008 05:08 AM
grep question - match exact string Panagiotis_IOA Linux - General 1 02-13-2008 10:38 AM
grep exact letters packets Linux - Newbie 1 11-30-2007 08: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 07:27 AM
grep [exact phrase] chrisfirestar Linux - General 2 02-09-2004 04:30 AM


All times are GMT -5. The time now is 02:15 AM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration