nv-l
[Top] [All Lists]

Grep, answered

To: nv-l@lists.tivoli.com
Subject: Grep, answered
From: Alex_North-Keys@TIVOLI.COM
Date: Fri, 8 Jan 1999 18:00:24 -0600
Reply-to: Discussion of IBM NetView and POLYCENTER Manager on NetView <NV-L@UCSBVM.UCSB.EDU>
Sender: Discussion of IBM NetView and POLYCENTER Manager on NetView <NV-L@UCSBVM.UCSB.EDU>
Hi, Lucy.  Lucy's question was:
 There's probably a very simple answer to this question,
 but I cannot for the life of me figure it out.
 How do you grep for an EXACT match of a string?
[data (abbreviated) was:]
 10.1.     #Bridgewater
 10.10.  #Scranton
[etc...]
 I'm trying to grep for the exact match of 10.1. in a script.
 That string will actually be in a variable defined previously
 in the script. When the grep runs, it outputs every line containing
 10.1?.  How do I prevent that from happening?
 - - -
Answer:

grep actually stands for Global Regular Expression Print, where the
Reg'Exp' is a description of the string to match.
(From the editor ed(1), use: g/regexp/p (to view lines with "foo"))
The extra things that need to be addressed in the regexp are:
1) You need to match the beginning of line
2) You want to see whitespace immediately after the "10.1."
3) You don't want to use simply "." for periods,
   because a period in a regexp really means "match any single character".

You need:  grep '^10\.1\.[    ]'

...where the gap between the square brackets [] is made up of one space
and one tab in either order.  The carat "^" matches the beginning of line.
The backslash "\" tells grep that the following period only is to match
periods.

The backslashes can cause difficulties in shell scripts, you may prefer:

     '^10[.]1[.][   ]'

Square-bracketed bits match one character position of the input,
where the matched character must be one of the ones between the
brackets.  Very few characters are still special (or "weird" if
you prefer) between brackets, just ^, -, and ].

You should use single quotes in the shell, since double quotes
still allow various substitutions on the quoted text, include
output-, variable-, and history-substitution.

When using $ to access the value of a shell variable, where the
exact composition of the whitespace in that variable matter,
enclose the $variable substitution in double quotes.  This keeps
whitespacing literally, instead of interpreting them as
command-argument splitters.

So in the end, you might have:

     regexp='^10[.]1[.][      ]'  # last [] contains space and tab
     grep "$regexp" filename

Other notes:  use a dollar sign to match end-of-line.  Example, to
match a line containing the word "foo" and ABSOLUTELY NOTHING ELSE, use:

     grep '^foo$'

-Alex.

<Prev in Thread] Current Thread [Next in Thread>
  • Grep, answered, Alex_North-Keys <=

Archive operated by Skills 1st Ltd

See also: The NetView Web