![脚本中发生次数计数错误](https://linux22.com/image/76677/%E8%84%9A%E6%9C%AC%E4%B8%AD%E5%8F%91%E7%94%9F%E6%AC%A1%E6%95%B0%E8%AE%A1%E6%95%B0%E9%94%99%E8%AF%AF.png)
我编写了一个小脚本来计算特定文件中日志的出现次数(以兆字节为单位),下面是我的代码
#!/bin/sh
count=1
while read line
do
found=$( grep M $line )
if [ "$found" ]; then
echo $count")" $line >> filesave.txt
fi
count=$((count+1))
done < log1.txt
但我收到类似 ./count.sh ./count.sh: 在第 10 行语法错误: `found=$' 意外的错误
答案1
grep M log1.txt |
cat -n |
sed 's/^[[:space:]]*\([0-9]\+\)[[:space:]]*/\1\)/' >> filesave.txt
sed
如果您对以下输出感到满意,则该脚本是可选的cat -n
grep M log1.txt | cat -n >> filesave.txt