grep -o '字符串' file |wc -l
awk -v RS="@#$j" '{print gsub(/字符串/,"&")}' file
awk '{s+=gsub(/字符串/,"&")}END{print s}' file
上述方法测试结果如下:
$ cat test
cat cat
cat
$ grep -o 't' test |wc -l
3
$ awk -v RS="@#$j" '{print gsub(/cat/,"&")}' test
3
$ awk '{s+=gsub(/cat/,"&")}END{print s}' test
3