Follow-up Comment #1, bug #22100 (project grep):

I've thought about this some more... here is another use case. 

Consider a directory of many files with contact details, of which 2 are
below. (This is a slightly contrived example, to illustrate my point.)

Sherlock_Holmes.txt contains:
 Name: Sherlock Holmes
 Address: 221b Baker Street
 Email:  [email protected]

Arthur_Wellesley.txt contains:
 Name: Arthur Wellesley
 Address: No 1, London
 Email:  [email protected]

etc etc.

I am trying to find the email address of Sherlock Holmes. So I need to find
the file containing "Sherlock Holmes", then look for the line beginning with
"Email", and then pull out the substring "[email protected]".

Currently, one must invoke 2 greps and a for loop. Eg:

 for file in *.txt; do
   if grep -q "Sherlock Holmes" $file; then
     email=$(grep -E "^Email:" $file | 
     grep -oiE "[a-z0-9_...@[a-z0-9_.]+")
   fi
 done
 echo "Email address is $email"


A, imho, more natural and expressive way to do this would be:

grep --file-match "Sherlock Holmes" --line-match "^Email" 
-oiE "[a-z0-9_...@[a-z0-9_.]+"  contacts/*


So I propose the following changes:

1. Iff grep is operating on multiple files, then 
   --file-match PATTERN 
  will filter on files that contain that pattern. 
  [If only a single file, or stdin, this option is ignored]

2. --line-match will match the line containing pattern.
   [If "-o" is not subsequently used, this is redundant]

3. Then -o can be used to match the relevant sub-section.

The -i and -E options would apply globally.


Note: please don't confuse my proposed "--line-match" with the existing, and
very different "--line-regexp". Also, please forgive the over-simplified RE
for an email address - it's just illustrative.


    _______________________________________________________

Reply to this item at:

  <http://savannah.gnu.org/bugs/?22100>

_______________________________________________
  Message sent via/by Savannah
  http://savannah.gnu.org/


Reply via email to