shell程序如下所示:
# cat xarg.txt #! /usr/bin/ksh for logfile in `find . -name "*.log*"` do echo "$logfile:" sed -n '/error/=' $logfile done files=($(find . -name "*log*")) echo "The length is ${#files[@]}" for file in ${files[@]} do echo "$file" awk '/error/{print NR}' $file done
#./xarg.txt
./dir/b.log:
1
2
3
4
./a.log:
1
3
4
The length is 2
./dir/b.log
1
2
3
4
./a.log
1
3
4
打印满足条件的行号的方法:
(1)在sed里面,/ERROR/表示匹配含有ERROR的行,=表示打印行号。
(2)在awk中,可以用awk '/error/{print NR}' $file,print NR表示打印满足条件的行号。