简体   繁体   English

计算目录中每个文件中的markdown链接

[英]Count markdown links in each file in a directory

I am trying to figure out how to count and display the number of markdown links in each file in a directory. 我试图弄清楚如何计算和显示目录中每个文件中的markdown链接数。

For an individual file, I can use grep and wc : 对于单个文件,我可以使用grepwc

grep -o -P "\[.*?\]\(.*?\)" file1.md | wc -l

But how can I do this individually for every file in a directory? 但是,如何对目录中的每个文件分别执行此操作? The following (and variations using ls and other commands) gives me a total for all files: 以下内容(以及使用ls和其他命令的变体)为我提供了所有文件的总计:

find . -name "*.md" | xargs grep -o -P "\[.*?\]\(.*?\)" | wc -l

Ultimately I would like something that resulted in a list showing the filename and the number of matches returned by grep, something like: 最终,我希望生成一个列表,该列表显示文件名和grep返回的匹配数,例如:

file1: 7
file2: 11

Though the exact formatting of the result isn't important 尽管结果的确切格式并不重要

How about a simple for loop? 简单的for循环怎么样?

for file in *.md
do
    echo -n ${file};grep -o -P "\[.*?\]\(.*?\)" ${file} | wc -l
done

Would the -c option, which produces a count of matches, be of help? 产生匹配计数的-c选项会有所帮助吗? as in

find . -name "*.md" | xargs grep -oc -P "\[.*?\]\(.*?\)" 

Can you try this one: 你能试试这个吗:

grep -o -P "\[.*?\]\(.*?\)" *md | cut -d ":" -f 1 | sort | uniq -c

it will give output like this: 它将给出如下输出:

     7 file1
    11 file2

If you want, you can transform output using sed 如果需要,可以使用sed转换输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM