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 04-05-2006, 08:41 PM   #1
zzytech
LQ Newbie
 
Registered: Apr 2006
Posts: 3

Rep: Reputation: 0
Help Use Grep Command, Thanks!


Hi all,

I am a new start of Linux. Have some problem when read the manpage of grep.

If use grep -w 'abc' file.txt, I got the match lins like:

abc xxx
xxx abc xxx
xxx abc

That's mean -w option match whole words. But when I read manpage, it tells "substring must either be at the beginning of the line" and "it must be either at the end of the line". Why does the above second line match?

Furthermore, in the manpage's regular expressions part, all talk about empty string.

"The caret ^ and the dollar sign $ are metacharacters that respectively match the empty string at the beginning and end of a line. The symbols \< and \> respectively match the empty string at the beginning and end of a word. The symbol \b matches the empty string at the edge of a word, and \B matches the empty string provided it's not at the edge of a word. "

I only know '^$' can match a empty line, and '^a' match a line start with 'a', 'c$' match line end with 'c' and '\<abc\>' just like -w 'abc' which matchs whole word 'abc'. But how to unstand them for empty string?

Anybody can give example for the use of \b and \B, that will be great help!

Thanks!

Vincent
 
Old 04-05-2006, 09:17 PM   #2
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

Rep: Reputation: 168Reputation: 168
The man page for grep in describing -w says 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. That means that non-word characters (including the spaces around the word) as well as the start of line or end of line will match.

If you have a file with the following contents:
Code:
Apache:
./configure --with-layout=Apache --prefix=/usr/local/apache2 --enable-rule=SHARED_CORE --enable-so --enable-ssl --enable-rewrite --with-ssl=/usr/include
make
make install
Restore from backups:
  /etc/rc.d/rc.httpd
  /etc/logrotate.d/httpd
  /usr/local/apache2/conf (including certs)
  /usr/local/apache2/cgi-bin
  /usr/local/apache2/modules
  /usr/local/apache2/passwd
Here are some examples since they'll show it better than I could explain it:
Code:
$ grep -iEn 'apache' test.txt 
1:Apache:
2:./configure --with-layout=Apache --prefix=/usr/local/apache2 --enable-rule=SHARED_CORE --enable-so --enable-ssl --enable-rewrite --with-ssl=/usr/include
8:  /usr/local/apache2/conf (including certs)
9:  /usr/local/apache2/cgi-bin
10:  /usr/local/apache2/modules
11:  /usr/local/apache2/passwd
Code:
$ grep -iEn '\<apache\>' test.txt 
1:Apache:
2:./configure --with-layout=Apache --prefix=/usr/local/apache2 --enable-rule=SHARED_CORE --enable-so --enable-ssl --enable-rewrite --with-ssl=/usr/include
Code:
$ grep -iEn '\bapache\b' test.txt 
1:Apache:
2:./configure --with-layout=Apache --prefix=/usr/local/apache2 --enable-rule=SHARED_CORE --enable-so --enable-ssl --enable-rewrite --with-ssl=/usr/include
Code:
$ grep -iEn '\bapache\B' test.txt 
2:./configure --with-layout=Apache --prefix=/usr/local/apache2 --enable-rule=SHARED_CORE --enable-so --enable-ssl --enable-rewrite --with-ssl=/usr/include
8:  /usr/local/apache2/conf (including certs)
9:  /usr/local/apache2/cgi-bin
10:  /usr/local/apache2/modules
11:  /usr/local/apache2/passwd
Hope that helps...
 
Old 04-05-2006, 11:37 PM   #3
zzytech
LQ Newbie
 
Registered: Apr 2006
Posts: 3

Original Poster
Rep: Reputation: 0
Great Help!
 
  


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
how to use grep command sharonyiisl Linux - Newbie 7 05-28-2006 03:46 PM
grep command itz2000 Linux - Newbie 2 09-21-2005 07:06 PM
grep command in c????? alnreddy Linux - Software 1 12-30-2004 01:01 AM
Help With GREP Command juliettree Linux - Newbie 3 04-08-2004 08:44 AM
grep command cuss Linux - General 7 02-14-2003 09:23 AM

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

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