I have a list of filtered files found by grep
by a specific regex, but want to use the results of that filtered list of files to further grep
those results only.
Filtering a Magento 2 code base for file names only: grep -Ril
(-i only because I tried to use extra filtering on regex)
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
Trying to add function
as testing my regex results into the regex however doesn’t return any results:
grep -Ril 'catalog_product_index_eav.*function.*' vendor/
grep -Ril 'catalog_product_index_eav.*.*function.*' vendor/
Either I need to be able to to handle the regex better which is not returning anything (function
keyword is just something that I test with since it should be returning at least something) or better:
How do I forward the result names to further filter:
I want to do something like this then on those filtered file names:
grep -Ril 'catalog_product_index_eav' vendor/ | grep 'function`
OR
grep -Ril 'catalog_product_index_eav' vendor/ | grep 'function` [$filename]
2
Answers
Suggesting to
awk
script toAND
operation on RegExp (actually any logical expression of one or more RegExp).The advantage of this approach: every file is scanned only once. And every file is scanned as a single record (
grep
is scanning line by line).The disadvantages: RegExp patterns in
awk
are case sensitive. Andawk
takes list of files only (no recursive folders).As for pure
grep
Suggesting to nest
grep
commands.Don’t use grep to find files. Use
find
to find files andgrep
to g/re/p (Globally match a Regular Expression and Print the results) within files. There are subtle clues in the tools’ names as to their intended purpose!In this case, though, what you apparently want to do within a file (match strings on different lines) is more than a simple g/re/p so then we turn to awk.
It sounds like this is what you’re trying to do, using GNU find for
+
and GNU awk forIGNORECASE
andnextfile
, and without reading the whole of each file into memory (as that could fail given a huge input file and is inefficient if the matches strings occur early in the file):If you didn’t have GNU tools you could do the same less efficiently with: