以下兩個指令用於計算文字檔案中第二列的非零整數的數量。誰能詳細解釋一下正規表示式。
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 統計匹配行數
第二個正規表示式匹配
'/ (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)