特定の文字のインスタンスを含むすべての行を Grep

特定の文字のインスタンスを含むすべての行を Grep

1行に「#」が1つだけあるすべての行をgrepしたいと思います。

例:

xxx#aaa#iiiii
xxxxxxxxx#aaa
#xxx#bbb#111#yy
xxxxxxxxxxxxxxxx#
xxx#x
#x#v#e#

この出力を提供する必要があります

xxxxxxxxx#aaa
xxxxxxxxxxxxxxxx#
xxx#x

ベストアンサー1

努力する

grep '^[^#]*#[^#]*$' file

どこ

^      ; begin of line
[^#]*  ; any number of char ≠ #
#      ; #
[^#]*  ; any number of char ≠ #
$      ; end of line

提案したようにライン全体をgrepすることができます。

grep -x '[^#]*#[^#]*'

そして

  • ラインアンカーの開始/終了がない同じパターンです。
  • -x行全体をgrepするには、次を参照してください。man grep
-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 $.

おすすめ記事