We have few scripts where we are using grep -w option to do exact matching of the pattern. This works fine on most of our servers.
But I have encounter a very old HP-UX System(HP-UX B.11.00) where grep -w option is not available.
This is causing my scripts to fail. I need to change my code to make it work on these old servers.
Can you guys help me with an alternative for grep -w to do exact word matching.
[user@host ~]$ cat file
the hello world pattern
the pattern hello world
the line without that word
pattern the hello world
the patternhello world
the hellopattern world
the hellopatternworld
[user@host ~]$
[user@host ~]$
[user@host ~]$ perl -ne '/\bpattern\b/ && print' file
the hello world pattern
the pattern hello world
pattern the hello world
[user@host ~]$
[^A-Za-z0-9_] replicates the "... non-word constituent character. Word- constituent characters are letters, digits, and the underscore" (cf man grep). This is ORed with begin-of-line ("^") or end-of-line ("$"), respectively. The -E option is necessary to accept the EREs.
---------- Post updated at 12:41 ---------- Previous update was at 12:39 ----------
In non-C locales, that regex may not be sufficient. The [[:alnum:]] class will take care of that, but may not be available on your system.
There are a number of grep-like codes that could be used in place of grep :
#!/usr/bin/env bash
# @(#) s1 Demonstrate alternate codes for "grep -w", ack, glark, peg, xtcgrep
# For ack, see:
# http://beyondgrep.com/
# For glark ( ruby ), see repository, or:
# https://github.com/jpace/glark
# For peg, see:
# http://www.cpan.org/authors/id/A/AD/ADAVIES/peg-3.10
# For xtcgrep, see:
# http://cpansearch.perl.org/src/MNEYLON/File-Grep-0.01/Grep.pm
# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C ack glark peg xtcgrep
FILE=${1-data1}
pl " Input data file $FILE:"
cat $FILE
for program in ack glark peg xtcgrep
do
pl " Results for program $program:"
$program -w abc $FILE
done
exit 0
producing:
$ ./s1
Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution : Debian 5.0.8 (lenny, workstation)
bash GNU bash 3.2.39
ack (local) 2.12
glark version 1.8.0
peg (local) 3.10
xtcgrep (local) 1.5
-----
Input data file data1:
abc
123abc456
123 abc
abc456
abc 456
-----
Results for program ack:
abc
123 abc
abc 456
-----
Results for program glark:
1 abc
3 123 abc
5 abc 456
-----
Results for program peg:
abc
123 abc
abc 456
-----
Results for program xtcgrep:
abc
123 abc
abc 456
Except as noted, the work-alike codes are written in perl . Some codes produce color and/or line numbers, both of which can be de-selected with options. Results shown are with default options.