現在のディレクトリの各サブディレクトリに、サブディレクトリの文字列とそのサブディレクトリ内のすべてのファイルに一致する行数を印刷したいと思います。
たとえば、私が持っている場合
cat /folder/a/file1.txt
test
x
x
cat /folder/a/file2.txt
x
test
x
test
cat /folder/b/c/file3.txt
x
test
x
test
/folder
inとすべてのサブディレクトリの発生回数を見たいです。ディレクトリごとに1行です。予想出力:
/folder: 4
/folder/a: 3
/folder/b: 1
/folder/b/c: 1
ベストアンサー1
これはもう一つのトリックです。 stdoutを使用してすべてのサブディレクトリから再帰的な合計をキャプチャするため、stderrを乱用して結果を印刷します。
function countdirhelper {
count=0
string=$1
for f in *
do
if [ -f "$f" ]
then
add=$(grep -c -- "$string" "$f")
elif [ -d "$f" ]
then
add=$(cd "$f"; countdirhelper "$string")
fi
count=$((count + add))
done
printf "%s: %d\n" "$PWD" "$count" >&2
printf %d "$count"
}
function countdir {
countdirhelper "$1" > /dev/null
}