In the following example:

apt-file search apache2.conf | grep -E "apache2.conf\b"


apache2: /etc/apache2/apache2.conf
apache2-doc: /usr/share/doc/apache2-doc/examples/apache2/apache2.conf.gz
dicoweb: /etc/dicoweb/apache2.conf
emboss-explorer: /etc/apache2/conf-available/emboss-explorer.apache2.conf
emboss-explorer: /etc/emboss-explorer/apache2.conf
icinga-cgi: /usr/share/doc/icinga-cgi/examples/apache2.conf
icinga-cgi: /usr/share/icinga/apache2.conf
icinga2-classicui: /etc/icinga2-classicui/apache2.conf
kopano-webapp-apache2: /etc/apache2/conf-available/kopano-webapp-apache2.conf
kopano-webapp-apache2: /etc/kopano/webapp/apache2.conf
lacme: /etc/lacme/apache2.conf
lemonldap-ng-handler: /etc/apache2/sites-available/handler-apache2.conf
libjs-twitter-bootstrap: /usr/share/twitter-bootstrap/apache2.conf
liblemonldap-ng-manager-perl: /etc/apache2/sites-available/manager-apache2.conf
liblemonldap-ng-portal-perl: /etc/apache2/sites-available/portal-apache2.conf
mirmon: /usr/share/doc/mirmon/examples/mirror-apache2.conf
nagios3-cgi: /usr/share/nagios3-cgi/apache2.conf
oar-restful-api: /usr/share/oar/oar-api/apache2.conf
octopussy: /etc/octopussy/apache2.conf
spip: /usr/share/doc/spip/apache2.conf

Why doesn't grep understand the \b as word boundary and incorporates this line:

apache2-doc: /usr/share/doc/apache2-doc/examples/apache2/apache2.conf.gz

Same result occurs with and without the usage of the extended (-E) version of grep.

  • use the -P instead of the -E option; \b is a perl/pcre thing. – mosvy Apr 20 at 16:24
  • thanks. the fact that \b works with -E version of sed confused me tbh; what is the word boundary equivalent for -E then? – pkaramol Apr 20 at 16:25
  • \< and \> should work in grep either with -E or with old-style regexps. – mosvy Apr 20 at 16:26
  • 1
    try with grep -P 'apache2\.conf(?=\s|$)' if you want conf to only match before a whitespace or at the end of the line. – mosvy Apr 20 at 16:31
  • 1
    even better and more idiomatic would be grep -P 'apache2\.conf(?!\S)' using a zero-width negative look-ahead assertion -- meaning "conf not followed by a non-space". – mosvy Apr 20 at 17:41

A word boundary is a transition from a word character to a non-word character - that's not the same as a transition from a word character to whitespace.

From man grep:

Word-constituent characters are letters, digits, and the underscore.

Since . is a non-word character conf\b matches conf.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.