LoginSignup
1
1

More than 1 year has passed since last update.

grepで正規表現をエスケープする

Posted at

#個人的なメモですみません。

背景

grepしていて、ドット.が正規表現として解釈されちゃうなぁと思い、エスケープしようとバックラッシュ\をつけたが上手くいかず、調べた。
#正規表現は勉強中。。

foo@gpu:~$ docker images | grep 3\.7
erudikaltd/scoold                                                                         latest                                276a123371e3        16 months ago       167MB
tensorflow/tensorflow                                                                     1.12.3-gpu-py3                        9b669e82b6ac        22 months ago       3.79GB
python                                                                                    3.7.3-stretch                         59a8c21b72d4        2 years ago         929MB
jupyterhub                                                                                latest                                c4bced83878a        2 years ago         9.05GB

この例では、3.7でひっかけようとして、337387が引っかかってしまった。

結論

エスケープは、引用符の中でしか通用しないらしい。

foo@gpu:~$ docker images | grep "3\.7"
tensorflow/tensorflow                                                                     1.12.3-gpu-py3                        9b669e82b6ac        22 months ago       3.79GB
python                                                                                    3.7.3-stretch                         59a8c21b72d4        2 years ago         929MB

-Fオプションをつけることで強制的に解釈をとめることができることもググって学んだ。

foo@gpu:~$ docker images | grep -F 3.7
tensorflow/tensorflow                                                                     1.12.3-gpu-py3                        9b669e82b6ac        22 months ago       3.79GB
python                                                                                    3.7.3-stretch                         59a8c21b72d4        2 years ago         929MB
1
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1