[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[bug #22100] Enhancement request: "grep foo | grep bar" in one process (
From: |
Richard Neill |
Subject: |
[bug #22100] Enhancement request: "grep foo | grep bar" in one process (i.e. line-selector and pattern selector) |
Date: |
Wed, 24 Nov 2010 01:28:50 +0000 |
User-agent: |
Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.11) Gecko/20101028 Mandriva Linux/1.9.2.11-0.2mdv2010.1 (2010.1) Firefox/3.6.11 |
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: address@hidden
Arthur_Wellesley.txt contains:
Name: Arthur Wellesley
Address: No 1, London
Email: address@hidden
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 "address@hidden".
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 "address@hidden")
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 "address@hidden" 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/
- [bug #22100] Enhancement request: "grep foo | grep bar" in one process (i.e. line-selector and pattern selector),
Richard Neill <=