Bash计算未加注释的行数,并以文件名写到输出。

问题描述 投票:0回答:1

我想计算多个文件中的非注释行,并将结果追加到一个输出文件中。

这是我如何计算多个文件的非注释行,但我不知道如何将结果与文件名一起存储在output.txt文件中。

for file in *txt
do
  cat "$file" | sed '/^\s*#/d' | wc -l
done
bash
1个回答
2
投票

你可以每行写几个东西,你可以把整个循环的输出结果重定向到一个文件中。

for file in *txt
do
  echo -n $file' '
  cat "$file" | sed '/^\s*#/d' | wc -l
done > output.txt

也可以把文件处理缩短为:

egrep -v '^\s*#' "$file" | wc -l

0
投票
for file in *txt
do
  cat "$file" | sed '/^\s*#/d' | wc -l >> output.txt
done
© www.soinside.com 2019 - 2024. All rights reserved.