Grep - 実際の固定文字列の検索

Grep - 実際の固定文字列の検索

固定パターン/文字列を一致させようとしています。

print
int

この例では、grep -F 'int'orを使用すると、grep -F "int"通常-Fフラグで「固定」文字列が生成されます。

intただし、この例では両方の文字列を一致させます(単にwith行を一致させる代わりにprint)。

他の多くの投稿にも同様の目標/問題があるようですが、その投稿で推奨される-Fフラグが機能しないことがここに明確に記載されています。

実際の固定文字列を一致させるためにgrepをどのように使用できますか?

ベストアンサー1

-F正規表現メタ文字などの.文字解釈をオフにし、*代わりに文字列リテラルとして扱います。パターンが部分文字列、全単語、または行全体と一致するかどうかには影響しません。これには、-wまたは-xフラグが必要です。

   -w, --word-regexp
          Select  only  those  lines  containing  matches  that form whole
          words.  The test is that the matching substring must  either  be
          at  the  beginning  of  the  line,  or  preceded  by  a non-word
          constituent character.  Similarly, it must be either at the  end
          of  the  line  or  followed by a non-word constituent character.
          Word-constituent  characters  are  letters,  digits,   and   the
          underscore.  This option has no effect if -x is also specified.

   -x, --line-regexp
          Select  only  those  matches  that exactly match the whole line.
          For a regular expression pattern, this  is  like  parenthesizing
          the pattern and then surrounding it with ^ and $.

おすすめ記事