次の2つのコマンド正規表現を詳しく説明できる人は誰ですか?

次の2つのコマンド正規表現を詳しく説明できる人は誰ですか?

次の2つのコマンドは、テキストファイルの2番目の列にあるゼロ以外の整数の数を計算するために使用されます。誰でも正規表現を詳しく説明できますか?

grep -c '^[^,]*,[^0][^,]*,' < myfile.txt
sed '/^[^,]*,0,.*/d' < myfile.txt | sed -n '$='

ベストアンサー1

最初の正規表現は、以下を含むすべての行を検索します。

'^        - start of line, followed by
 [^,]*    - 0 or more non-comma characters, followed by
 ,        - a comma, followed by
 [^0]     - any single character other than a zero, followed by
 [^,]*    - 0 or more non-comma characters, followed by
 ,'       - a comma

grep -c 一致する行数を計算します。

2番目の正規表現が一致します。

'/        (the start of the regex)
 ^        - start of line, followed by
 [^,]*    - 0 or more non-comma characters, followed by
 ,0,      - a comma then a zero then a comma, followed by
 .*       - 0 or more other characters
 /d'      (the end of the regex -- delete the lines matching the preceding expression)

おすすめ記事