grep正規表現を使用した2つのディレクトリの除外

grep正規表現を使用した2つのディレクトリの除外

これSEの答えは、正規表現とgrepを使ってディレクトリを除外できることに気づきました。ただし、以下を使用すると機能しません|

results=$(grep --color=always -rnF "$searchTerm" . --exclude-dir='.[^.]*|node_modules')
results=$(grep --color=always -rnF "$searchTerm" . --exclude-dir='.[^.]*\|node_modules')

ピリオドと node_modules で始まるディレクトリが除外されるように、この正規表現を作成する別の方法はありますか?

ベストアンサー1

少なくともGNU grepの場合、--exclude正規表現ではなくグローバルパターンが必要なようです。リンクした質問に対する答えは、それが言うことを意味します。" pcregrep と grep で --exclude-dir の意味が異なることに注意してください。。具体的には:

存在するman grep

  --exclude-dir=GLOB
          Skip any command-line directory with a name suffix that  matches
          the   pattern   GLOB.   When  searching  recursively,  skip  any
          subdirectory whose base name matches GLOB.  Ignore any redundant
          trailing slashes in GLOB.

存在するman pcregrep

   --exclude=pattern
             Files (but not directories) whose names match the pattern are
             skipped  without  being processed. This applies to all files,
             whether listed on the command  line,  obtained  from  --file-
             list, or by scanning a directory. The pattern is a PCRE regu‐
             lar expression, and is matched against the final component of
             the  file  name,  not the entire path.

少なくともGNU grepでは、--exclude-dir複数のパターンを除外したい場合は複数回使用できます。

--exclude-dir='.?*' --exclude-dir='node_modules'

私はそれを.[^.]*次のように変更しました.?*

  • ここで現在のディレクトリから検索しているので、それが意図かどうかを除外しないように努力することは意味がありません(オブジェクトファイルを省略すると、基本的に最新バージョンのGNUに設定されます。..ただし、少なくともGNU 3.11では除外しないという事実が見つかりました)、したがってターゲットディレクトリが指定されていない場合は、このバージョンで十分です。-r.grepgrep--exclude-dir='*'--exclude-dir='.*'
  • ..fooこれは、または名前付きディレクトリを除外しません。...
  • 正規表現に対応するPOSIX globは次[^.]のとおりです[!.][^.]少なくともGNUシステムでは通常サポートされていますが)。

おすすめ記事