DEV Community

loading...

【Mac】Regular Expressions for exact matching as words with grep and sed

kamekuremaisuke profile image t-o-d ・2 min read
  • 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.

Result

  • 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

10,30,50,100,150,250,300,500

# grep only 50 of the data examples
$ cat data | grep -o '[[:<:]]50[[:>:]]'

50

# Replace only 50 of the example data with "match".
$ cat data.txt | sed 's;[[:<:]]50[[:>:]];match;g'

10,30,match,100,150,250,300,500
Enter fullscreen mode Exit fullscreen mode

Description

  • The above example shows the process of search and replace for arbitrary data.
  • The method used here is to enclose the word like [[:<:]]word[[:>:]].

grep

  • 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

50
50
50
50
Enter fullscreen mode Exit fullscreen mode
  • 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.

sed

  • The same is true for sed. If you replace it with the string as it is, it will be replaced 4 times.
$ cat data.txt | sed 's;50;match;g'

10,30,match,100,1match,2match,300,match0
Enter fullscreen mode Exit fullscreen mode
  • From these things, it is necessary to recognize it as a word like [[:<:]]word[[:>:]] like grep.

Conclusion

  • From the above, I was reminded of further study of regular expressions, conscious of data duplication.

Link

Discussion (0)

pic
Editor guide