Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 如何使用for命令按文件向我提供输出?_Bash_Macos_Unix - Fatal编程技术网

Bash 如何使用for命令按文件向我提供输出?

Bash 如何使用for命令按文件向我提供输出?,bash,macos,unix,Bash,Macos,Unix,我有一个名为Chapters的文件夹,其中包含20个文件,每个文件包含一本书的一章 我有一个名为book_chap.list的文件,其中包含章节列表。它包含如下内容: chap_00 chap_01 chapters/chap_01:Name1 chapters/chap_01:Name1 chapters/chap_01:Name2 我有第三个名为book.names10的文件,其中包含一个名称列表。它包含如下内容: Name1 Name2 我从输出中需要的是一个文件,该文件按章节指示

我有一个名为Chapters的文件夹,其中包含20个文件,每个文件包含一本书的一章

我有一个名为book_chap.list的文件,其中包含章节列表。它包含如下内容:

chap_00
chap_01 
chapters/chap_01:Name1
chapters/chap_01:Name1
chapters/chap_01:Name2
我有第三个名为book.names10的文件,其中包含一个名称列表。它包含如下内容:

Name1
Name2
我从输出中需要的是一个文件,该文件按章节指示每个名字在每个章节中的出现时间。大概是这样的:

chap_00
chap_01 
chapters/chap_01:Name1
chapters/chap_01:Name1
chapters/chap_01:Name2
我用的是:

for a in chapters/chap_* ;
  do
    echo -n $a;
    ggrep -F -f book.names10 -w -o $a | wc -l ;
done

但我得到的唯一一件事是一份清单,上面列出了这些名字在每一章中的使用次数。我不知道在何处集成此命令上的文件簿\u chap.list

又快又脏您可能想要美化输出:

#!/bin/bash
for chapter in $(ls chapters/chap_*); do
    echo "Chapter ${chapter}:" >> nameOccurences.txt
    for name in $(cat book.names10); do
            echo -n "${name}: " >> nameOccurences.txt
            grep -o ${name} ${chapter} | wc -l >> nameOccurences.txt
    done
echo "" >> nameOccurences.txt
done

如果要按名称而不是章节对结果进行分组,则必须相应地交换循环和输出

名称1是否包含空格?名称和名称的计数应该相同吗?您在代码中使用的book.ents10是什么?如果你能知道你在问题中提到的文件的确切位置,那就太好了,这还不清楚。此外,预期的输出似乎与您的描述不匹配。不,它不计算空格,只计算名称。我编辑了主帖子book.ents10应该是book.names10,我的makefile有第一个,我忘了把它改成第二个。现在已经修好了,很抱歉给他们带来了混乱!Book.names10是一个文件,其中包含贯穿各章的名称。book_chap.list和book.names10位于一个目录中,在该目录中还有另一个名为chapters的目录,其中包含已划分章节的文件?没有for循环-只需grep,多个输入文件由glob pattern chapters/chap.*匹配。如果搜索所有章节文件,则book_chap.list似乎不必要。全局模式将扩展为这些文件的列表。