以下两个命令用于计算文本文件中第二列的非零整数的数量。谁能详细解释一下正则表达式。
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)