我想计算多个文件中的非注释行,并将结果追加到一个输出文件中。
这是我如何计算多个文件的非注释行,但我不知道如何将结果与文件名一起存储在output.txt文件中。
for file in *txt
do
cat "$file" | sed '/^\s*#/d' | wc -l
done
你可以每行写几个东西,你可以把整个循环的输出结果重定向到一个文件中。
for file in *txt
do
echo -n $file' '
cat "$file" | sed '/^\s*#/d' | wc -l
done > output.txt
也可以把文件处理缩短为:
egrep -v '^\s*#' "$file" | wc -l
for file in *txt
do
cat "$file" | sed '/^\s*#/d' | wc -l >> output.txt
done