LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Help Use Grep Command, Thanks! (https://www.linuxquestions.org/questions/linux-newbie-8/help-use-grep-command-thanks-432144/)

zzytech 04-05-2006 08:41 PM

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

gilead 04-05-2006 09:17 PM

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...

zzytech 04-05-2006 11:37 PM

Great Help!


All times are GMT -5. The time now is 02:16 PM.