LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 03-28-2013, 02:55 AM   #1
thomas2004ch
Member
 
Registered: Aug 2009
Posts: 488

Rep: Reputation: 31
grep a word from a file


Hi,

The file contains follinwg line:
Code:
...
s003ap12.test
...
But I just want to know if the file contains only the 's003ap12', not the s003ap12.test. So I use the following command:
Code:
grep -w s003ap12 myfile
But it returns possitive result. What a option should I use?
 
Old 03-28-2013, 03:14 AM   #2
kooru
Senior Member
 
Registered: Sep 2012
Location: Italy
Distribution: Slackware, NetBSD
Posts: 1,252
Blog Entries: 4

Rep: Reputation: 259Reputation: 259Reputation: 259
Hi,

from man grep

Code:
-w, --word-regexp
Select  only  those  lines containing matches that form whole words.  The test is that the matching substring must
either be at the beginning of the line, or preceded by a non-word constituent character.  Similarly,  it  must  be
either  at  the  end of the line or followed by a non-word constituent character.  Word-constituent characters are
letters, digits, and the underscore.
Try with

Code:
grep "^s003ap12$" myfile
 
1 members found this post helpful.
Old 03-28-2013, 04:22 AM   #3
thomas2004ch
Member
 
Registered: Aug 2009
Posts: 488

Original Poster
Rep: Reputation: 31
Quote:
Originally Posted by kooru View Post
Hi,

Try with

Code:
grep "^s003ap12$" myfile

It doesn't work. Have you tried yourself?
 
Old 03-28-2013, 04:30 AM   #4
kooru
Senior Member
 
Registered: Sep 2012
Location: Italy
Distribution: Slackware, NetBSD
Posts: 1,252
Blog Entries: 4

Rep: Reputation: 259Reputation: 259Reputation: 259
Of course

File called bbb with this entry

s003ap12.test
s003ap12
s003ap12aaaa

i now use option "-n" to have the number of row that match

Code:
grep -n "^s003ap12$" bbb
2:s003ap12
or maybe i've understand not well what you want?
 
Old 03-28-2013, 04:44 AM   #5
thomas2004ch
Member
 
Registered: Aug 2009
Posts: 488

Original Poster
Rep: Reputation: 31
Quote:
Originally Posted by kooru View Post
Of course

File called bbb with this entry

s003ap12.test
s003ap12
s003ap12aaaa

i now use option "-n" to have the number of row that match

Code:
grep -n "^s003ap12$" bbb
2:s003ap12
or maybe i've understand not well what you want?
Hi,

You are right. But this can only work if the word at the beginning. Otherwise it doesn't work. If I change the content of the file as follow:
Code:
12 003ap12.test
23 s003ap12
34 s003ap12aaaa
It will not work.
 
Old 03-28-2013, 04:50 AM   #6
kooru
Senior Member
 
Registered: Sep 2012
Location: Italy
Distribution: Slackware, NetBSD
Posts: 1,252
Blog Entries: 4

Rep: Reputation: 259Reputation: 259Reputation: 259
In your case try:

Code:
grep "s003ap12$" myfile
If you don't want the first colulmn, try:

Code:
grep "s003ap12$" myfile | awk '{print $2}'
 
2 members found this post helpful.
Old 03-28-2013, 04:55 AM   #7
thomas2004ch
Member
 
Registered: Aug 2009
Posts: 488

Original Poster
Rep: Reputation: 31
Quote:
Originally Posted by kooru View Post
In your case try:

Code:
grep "s003ap12$" myfile
If you don't want the first colulmn, try:

Code:
grep "s003ap12$" myfile | awk '{print $2}'
Hi,

My case is just an example. In the fact you don't know in which colum the word 's003ap12' locates. Maybe it will appear in colume 2, or 3 or at the beginning.


But anyway, I am satified with your answer.

Last edited by thomas2004ch; 03-28-2013 at 04:57 AM.
 
Old 03-28-2013, 05:03 AM   #8
thomas2004ch
Member
 
Registered: Aug 2009
Posts: 488

Original Poster
Rep: Reputation: 31
Wait a moment please.

I now change the content of the file as follow:
Code:
12 003ap12.test
23 s003ap12
34 s003ap12aaaa
23 s003ap12-test.proactiv.de
And I want to grep the 's003ap12-test'. How to do this?
 
Old 03-28-2013, 05:04 AM   #9
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,480

Rep: Reputation: 1220Reputation: 1220Reputation: 1220Reputation: 1220Reputation: 1220Reputation: 1220Reputation: 1220Reputation: 1220Reputation: 1220
grep -F 's003ap12-test'
 
Old 03-28-2013, 07:13 AM   #10
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
grep's regex also supports four special zero-width patterns: "\<","\>","\b", and "\B".

The first three match the zero-width transition between a word and a non-word character. The arrows only match in one direction (i.e. [nonword]\<[word], and [word]\>[nonword]), while "\b" matches both. They can also match at the beginning or ending of the string, like "^" and "$".

Code:
grep '\<s003ap12-test\>'
grep '\bs003ap12-test\b'

"\B" is the opposite of "\b", and only matches the transition between two characters of the same type, that is two word characters together or two non-word characters together. I only mention it for completeness, as it isn't useful here.

Finally, remember that a "word" character is a-z, A-Z, 0-9, and _ (underscore). This cannot be changed, so things can get trickier if you have to redefine what a word is.
 
1 members found this post helpful.
Old 03-28-2013, 07:43 AM   #11
suitianshi
LQ Newbie
 
Registered: Feb 2013
Posts: 14

Rep: Reputation: Disabled
I'm not very clear about what you want.
you can try this:

grep 's003ap12[^\.]*' myfile

it will not match s003ap12.test(since it has a ".")
 
Old 03-28-2013, 11:49 AM   #12
w1k0
Senior Member
 
Registered: May 2008
Location: Poland
Distribution: Slackware, Mint
Posts: 1,240

Rep: Reputation: 196Reputation: 196
thomas2004ch,

Show here:

1. A few variants of the lines including the desirable text (the lines to be found by grep).

2. A few variants of the lines including the undesirable text (the lines to be skipped by grep).

As a result someone will be able to help you. Now all the people try to guess what you mean.
 
  


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
finding a word by using grep in whole file system c2431993 Linux - Newbie 2 09-29-2010 06:38 PM
grep a word from some files??? shahz Red Hat 3 09-06-2008 03:48 AM
grep a word in a .tar.gz file prernabhagat Linux - General 1 12-07-2007 01:53 AM
Word count with grep DiagonalArg Linux - Software 3 02-13-2006 12:46 PM
grep data between two word DropSig Linux - General 2 01-27-2006 08:31 PM


All times are GMT -5. The time now is 06:00 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 Google+: linuxquestions
Open Source Consulting | Domain Registration