使用 grep 过滤的文件列表,将该文件列表转发给进一步的 grep

Filtered list of files with grep, forward that list of files to grep further

我有一个由 grep 通过特定正则表达式找到的过滤文件列表,但我想使用 该过滤文件列表 的结果进一步 grep 仅这些结果。

仅为文件名过滤 Magento 2 代码库:grep -Ril(-i 只是因为我试图在正则表达式上使用额外的过滤)

 grep -Ril 'catalog_product_index_eav' vendor/


vendor/magento/module-catalog/Model/ResourceModel/Layer/Filter/Decimal.php
vendor/magento/module-catalog/Model/ResourceModel/Layer/Filter/Attribute.php
vendor/magento/module-catalog/Model/ResourceModel/Product/Indexer/Eav/Decimal.php
vendor/magento/module-catalog/Model/ResourceModel/Product/Indexer/Eav/Source.php
vendor/magento/module-catalog/etc/db_schema.xml
vendor/magento/module-catalog/etc/db_schema_whitelist.json
vendor/magento/magento2-base/dev/tests/static/testsuite/Magento/Test/Integrity/_files/dependency_test/tables_ce.php
vendor/magento/magento2-base/dev/tests/integration/testsuite/Magento/Catalog/Model/ResourceModel/Product/Indexer/Eav/SourceTest.php
vendor/magento/magento2-base/dev/tests/integration/testsuite/Magento/CatalogSearch/Model/Search/FilterMapper/StockStatusFilterWithFullFilterTest.php
vendor/magento/magento2-base/dev/tests/integration/testsuite/Magento/CatalogSearch/Model/Search/FilterMapper/StockStatusFilterWithGeneralFilterTest.php
vendor/magento/magento2-base/dev/tests/integration/testsuite/Magento/CatalogSearch/Model/Search/FilterMapper/CustomAttributeFilterTest.php
vendor/magento/magento2-base/dev/tests/integration/testsuite/Magento/CatalogSearch/Model/Search/FilterMapper/VisibilityFilterTest.php
vendor/magento/magento2-base/dev/tests/integration/testsuite/Magento/CatalogSearch/Model/Adapter/Mysql/BaseSelectStrategy/BaseSelectAttributesSearchStrategyTest.php
vendor/magento/module-catalog-search/Model/Search/FilterMapper/VisibilityFilter.php
vendor/magento/module-catalog-search/Model/Search/FilterMapper/CustomAttributeFilter.php
vendor/magento/module-catalog-search/Model/Search/FilterMapper/TermDropdownStrategy/SelectBuilder.php
vendor/magento/module-catalog-search/Model/Adapter/Mysql/Filter/Preprocessor.php
vendor/magento/module-catalog-search/Model/Adapter/Mysql/BaseSelectStrategy/BaseSelectAttributesSearchStrategy.php
vendor/magento/module-catalog-search/Model/Adapter/Mysql/Aggregation/DataProvider/QueryBuilder.php
vendor/magento/module-catalog-search/Model/Adapter/Mysql/Aggregation/DataProvider/SelectBuilderForAttribute.php
vendor/magento/module-catalog-search/Test/Unit/Model/Adapter/Mysql/Aggregation/DataProvider/QueryBuilderTest.php
vendor/magento/module-inventory-catalog-search/Test/Integration/Model/Search/FilterMapper/TermDropdownStrategy/ApplyStockConditionToSelectOnDefaultStockTest.php
vendor/magento/module-inventory-catalog-search/Test/Integration/Model/Search/FilterMapper/TermDropdownStrategy/ApplyStockConditionToSelectTest.php
vendor/magento/module-inventory-catalog-search/Test/Integration/Model/Adapter/Mysql/Aggregation/DataProvider/ApplyStockConditionToSelectWithDefaultStockTest.php
vendor/magento/module-inventory-catalog-search/Test/Integration/Model/Adapter/Mysql/Aggregation/DataProvider/ApplyStockConditionToSelectTest.php
vendor/magento/module-inventory-catalog-search/Test/_files/clean_catalog_product_index_eav_table.php

尝试添加 function 作为 testing my regex results into the regex 然而 return 没有任何结果:

grep -Ril 'catalog_product_index_eav.*function.*' vendor/
grep -Ril 'catalog_product_index_eav.*.*function.*' vendor/

要么我需要能够更好地处理正则表达式,这不是 returning 任何东西(function 关键字只是我测试的东西,因为它应该是 return至少有一些东西)或更好:

How do I forward the result names to further filter:

然后我想对那些过滤后的文件名执行类似的操作:

grep -Ril 'catalog_product_index_eav' vendor/ | grep 'function`
OR
grep -Ril 'catalog_product_index_eav' vendor/ | grep 'function` [$filename]

建议 awk 脚本 AND 对 RegExp 的操作(实际上是一个或多个 RegExp 的任何逻辑表达式)。

 awk '/regExp-pattern-1/ && /regExp-pattern-2/ {print FILENAME}' RS="&@&@&@&@" files-1 file-2 ...

这种方法的优点:每个文件只扫描一次。并且每个文件都被扫描为单个记录(grep 逐行扫描)。

缺点:awk 中的 RegExp 模式区分大小写。 awk 仅获取文件列表(无递归文件夹)。

至于纯grep

建议嵌套 grep 命令。

 grep -il "regExp-pattern-2" $(grep -irl "regExp-pattern-1" folder-path)

不要使用 grep 查找文件。使用 find 找到 文件,使用 grepg/re/p (G局部匹配文件中的R常规E表达式和P打印结果)。这些工具的名称中有关于其预期用途的微妙线索!

不过,在这种情况下,您显然想在一个文件中做的事情(匹配不同行上的字符串)不仅仅是简单的 g/re/p 所以我们转向 awk。

听起来这就是您要尝试做的事情,使用 GNU find + 和 GNU awk IGNORECASEnextfile,并且没有阅读全部将文件存入内存(因为如果输入文件很大,这可能会失败,如果匹配字符串出现在文件的早期,效率很低):

find vendor -type f -exec \
    awk -v IGNORECASE=1 'FNR==1{x=y=0} /catalog_product_index_eav/{x=1} /function/{y=1} x && y{print FILENAME; nextfile}' {} +

如果您没有 GNU 工具,您可以使用以下方法来降低效率:

find vendor -type f -exec \
    awk '{lc=tolower([=11=])} lc ~ /catalog_product_index_eav/{x=1} lc ~ /function/{y=1} x && y{print FILENAME; exit}' {} \;