【问题标题】:dynamic exclusion of files through grep matching通过grep匹配动态排除文件
【发布时间】:2019-04-19 11:02:19
【问题描述】:

我有一个文件 source-push.sh,它返回我想从 find 命令的结果中排除的文件列表。

看起来像这样:

#!/usr/bin/env bash
find . -not \( -path './node_modules' -prune \) -name '*.js' | grep -vE $(echo $(./source-push.sh | xargs -I{} echo -n "{}|") | rev | cut -b2- | rev) | xargs -L1 standard --fix
find . -not \( -path './node_modules' -prune \) -name '*.css' | grep -vE $(echo $(./source-push.sh | xargs -I{} echo -n "{}|") | rev | cut -b2- | rev) | xargs -L1 stylelint --config stylelint.json

应该有比这更好的方法来完成这项工作。有什么建议吗?

【问题讨论】:

    标签: grep find echo xargs


    【解决方案1】:

    代替:

    ... | grep -vE $(echo $(./source-push.sh | xargs -I{} echo -n "{}|") | rev | cut -b2- | rev ) | ...
    

    您可以使用POSIX 选项-F-f

    ... | grep -v -F -f <( ./source-push.sh ) | ...
    
    • -F 告诉 grep 模式是固定字符串
      • (避免如果模式包含grep -E 特有的字符,您的原始代码会中断的问题)
    • -f file 告诉 grep 使用来自 file 的模式列表
    • &lt;( ... ) 是一种将程序输出呈现为文件(命名管道)的 bash 方式

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-08
      • 1970-01-01
      • 1970-01-01
      • 2021-09-19
      • 2023-04-06
      • 1970-01-01
      相关资源
      最近更新 更多