如何计算某个单词在不同文件中出现的次数? -BASH

如何计算某个单词在不同文件中出现的次数? -BASH

我有像 raw_data_0、raw_data_1......raw_data_7 这样的文件,每个文件都包含以下数据

record_count=1
emailCountFailure=0
record_count=1
emailCountFailure=0
record_count=1
emailCountFailure=0
record_count=1
emailCountFailure=0
record_count=1
emailCountFailure=0
record_count=1
emailCountFailure=0
record_count=1
emailCountFailure=0
record_count=1
emailCountFailure=0

我需要打开所有文件并计算每个文件中有多少 record_count 和 emailCountFailure,最后将所有计数相加。

我尝试使用下面的代码遍历所有文件并尝试使用 grep 进行计数。但输出并不像预期的那样(如下)。

#!/bin/ksh
MY_WORK='/a585/app/data/CCN_text'
raw_data_file_count=`ls -l raw_data_*|wc -l`
echo " $raw_data_file_count"
#raw_data_file_counter=(`ls -l raw_data_* | awk -F" " '{print $9}' | awk -F"_" '{print $3}'`)

ls -l raw_data_* | awk -F" " '{print $9}' | awk -F"_" '{print $3}' > file_counter.txt

while read line
do

counter_array+=("$line")

done < file_counter.txt


raw_data_file_counter_length=${#counter_array[@]}

echo "ARRAY LENGTH : ${raw_data_file_counter_length}"

for ((count=0; count<${raw_data_file_counter_length}; count++));
do
echo " COUNTER VALUE: ${counter_array[$count]} "
reccount="$(grep 'record_count' $MY_WORK/raw_data_${counter_array[$count]})"

echo "$reccount"

done

输出:

 8
ARRAY LENGTH : 8
 COUNTER VALUE: 0
record_count=1
 COUNTER VALUE: 1
record_count=1
record_count=2
 COUNTER VALUE: 2
record_count=1
record_count=2
 COUNTER VALUE: 3
record_count=1
 COUNTER VALUE: 4
record_count=1
record_count=2
 COUNTER VALUE: 5
record_count=1
record_count=2
 COUNTER VALUE: 6
record_count=1
record_count=2
 COUNTER VALUE: 7
record_count=1

我需要将所有文件的“record_count”计数相加。有什么方法可以实现吗?

答案1

您可以使用-cgrep 的标志来计算文件中某个字符串的出现次数并循环遍历所有字符串。

#!/usr/bin/env bash

rc="0"
ecf="0"

for f in ./raw_data_*; do
    let "rc=rc+$(cat $f | grep -c 'record_count')"
    let "ecf=ecf+$(cat $f | grep -c 'emailCountFailure')"
done

echo "record_count = ${rc}"
echo "emailCountFailure = ${ecf}"

结果:

record_count = 24
emailCountFailure = 24

相关内容