skip to Main Content

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


  1. Suggesting to awk script to AND operation on RegExp (actually any logical expression of one or more RegExp).

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

    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. And awk takes list of files only (no recursive folders).

    As for pure grep

    Suggesting to nest grep commands.

     grep -il "regExp-pattern-2" $(grep -irl "regExp-pattern-1" folder-path)
    
    Login or Signup to reply.
  2. Don’t use grep to find files. Use find to find files and grep 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 for IGNORECASE and nextfile, 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):

    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}' {} +
    

    If you didn’t have GNU tools you could do the same less efficiently with:

    find vendor -type f -exec 
        awk '{lc=tolower($0)} lc ~ /catalog_product_index_eav/{x=1} lc ~ /function/{y=1} x && y{print FILENAME; exit}' {} ;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search