GNU文書によると:
‘\<’ Match the empty string at the beginning of word.
‘\>’ Match the empty string at the end of word.
私の/etc/fstabは次のようになります:
/dev/sdb1 /media/fresh ext2 defaults 0 0
私はgrepが/media/freshの存在に対してTRUE / FALSEを返したいと思います。\<
を試してみましたが、\>
成功しませんでした。なぜ?
egrep '\</media/fresh\>' /etc/fstab
解決策:
egrep '[[:blank:]]/media/fresh[[:blank:]]' /etc/fstab
しかし、もっと醜いです。
私のgrepは2.5.1です。
ベストアンサー1
\<
\>
単語の先頭と末尾でそれぞれ空の文字列と一致し、単語構成文字は次のとおりです。
[[:alnum:]_]
からman grep
:
Word-constituent characters are letters, digits, and the underscore.
したがって、/
正しい単語形成文字ではないため、正規表現は失敗します。
代わりに、単語の周囲にスペースがあるときに単語を一致させる-w
オプションを使用できます。grep
grep -wo '/media/fresh' /etc/fstab
例:
$ grep -wo '/media/fresh' <<< '/dev/sdb1 /media/fresh ext2 defaults 0 0'
/media/fresh