如果找到相应的文件,请过滤bash中的文件列表

发布于 2025-02-13 22:55:18 字数 1219 浏览 3 评论 0原文

我有一堆.js文件。由于具有相应的.spec.js有些具有.test.js文件,而有些则没有相应的文件。

我将规格文件转换为测试文件,但我想定位最经常更改的文件。

为此,我有一个查找git 中最更改的文件的bash命令。但是我想将返回的列表过滤到仅包含具有匹配.spec.js文件的文件。

这是现有命令:

git log --pretty=format: --name-only | sort | uniq -c | sort -rg | head -10

来自问题的代码,要求列表文件缺少相应的文件< /a>看起来很有帮助,

for f in *.ext; do test -e $f.txt || echo $f; done

这意味着我可以使用,

for f in $(git log --pretty=format: --name-only); do test -e ${f%.*}.spec.js && echo $f; done | sort | uniq -c | sort -rg | head -25

但是如果可能的话,我不想在我的git日志输出的每一行上循环,因为它显然包含重复项!这很快(〜1秒),但我觉得应该有一个“整洁”选项。

文件列表的答案几乎帮助但依赖于ls

有更好的方法来获取此输出吗?

I have a bunch of .js files. Since have corresponding .spec.js some have .test.js files and some have no corresponding files.

I am converting the spec files to test files, but I want to target the files that are most often changed first.

To do that I have a bash command that is Finding most changed files in Git but I want to filter the returned list to only include files that have a matching .spec.js file.

Here is the existing command:

git log --pretty=format: --name-only | sort | uniq -c | sort -rg | head -10

The code from the question asking to List files missing a corresponding file looks helpful

for f in *.ext; do test -e $f.txt || echo $f; done

Means I could use

for f in $(git log --pretty=format: --name-only); do test -e ${f%.*}.spec.js && echo $f; done | sort | uniq -c | sort -rg | head -25

But I don't want to loop over every line of my git log output if possible, because it obviously contains duplicates! It's quite quick (~1 sec) but I feel like there should be a 'neater' option.

The answers at Filter list of files to those that exist almost help but relies on ls.

Is there a better way to get this output?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

彡翼 2025-02-20 22:55:18

可能是这样的事情:
while ifs = read -r file;做回声$文件完成&lt; &lt;(git log -pretty -format:-name -inly | grep“ .spec.js” | sort | sort | uniq -c | sort -rg | head -25)>
这是您可以使用GREP过滤GIT日志结果的一种方法。然后,您可以在段循环中做任何您想做的事情。这是测试的。确保您之间没有空间;(

Possibly something like this:
while IFS= read -r file; do echo $file done < <(git log --pretty-format: --name-only | grep ".spec.js" | sort | uniq -c | sort -rg | head -25)
This is one way you could use grep to filter your git log result. Then you can do whatever you want in the while loop. This was tested. Make sure you have no space between these <(

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文