0

以下のような sample.txt ファイルがあります。

1.hi _plane_ is
2.hi airplane is
3.hi plane is
4.hi _plane- is
5.hi :plane: is
plane
there is a plane here

他の単語の中にない「飛行機」という単語、たとえば「飛行機」を見つけたいので、grepの後、出力は次のようになります(2行目のみが除外されます)

1.hi _plane_ is
3.hi plane is
4.hi _plane- is
5.hi :plane: is
plane
there is a plane here

私は試した:

grep -w "plane" sample.txt
grep "\bplane\b\|\Bplane\B" sample.txt

しかし、出力は私が期待するものと一致しません。正しい結果を得るには、実際にgrepをどのように使用すればよいですか?

どうもありがとう。

4

3 に答える 3

2
egrep -v '[[:alpha:]]+plane|plane[[:alpha:]]+' 
于 2013-01-24T04:13:33.483 に答える
0

これを試して:

grep "(^|[^a-zA-Z])plane([^a-zA-Z]|$)" sample.txt

の問題\bは、アンダースコア文字_が「単語文字」としてカウントされるため、\bplane\b一致しないことです_plane_

于 2013-01-24T04:10:42.647 に答える
0

GNU の場合grep:

grep -w plane sample.txt

それをスクラッチします。grep -wアンダースコアは単語の一部としてカウントされるため、このコンテキストでは役に立ちません。

于 2013-01-24T04:16:27.930 に答える