When we use grep or sed in our business, we often use word matching to search and replace.
In this case, it is not uncommon that the exact match is not found.
In this article, we will record the regular expression notation for doing so with a high probability of an exact match.
A description of the results is given first.
※It is designed to work with BSD-style grep and sed on Mac.
# Data Example$ cat data
# grep only 50 of the data examples$ cat data | grep-o'[[:<:]]50[[:>:]]'
# Replace only 50 of the example data with "match".$ cat data.txt | sed's;[[:<:]]50[[:>:]];match;g'
The above example shows the process of search and replace for arbitrary data.
The method used here is to enclose the word like [[:<:]]word[[:>:]].
In the first place, grep extracts not only words but also lines.
To extract only words, use option and use grep -o word.
However, in the above data example, there is a partial match as shown below, and four are extracted.
$ cat data | grep-o 50
From these things, we use the [[:<:]]word[[:>:]] which has the following meanings.
Since it matches the beginning and end of a word, it does not recognize the character strings before and after.
The same is true for sed. If you replace it with the string as it is, it will be replaced 4 times.