|
Home > Archive > Unix Shell > April 2004 > Regular expression: matching one string and not the other
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Regular expression: matching one string and not the other
|
|
| Timur Tabi 2004-04-20, 8:35 pm |
| I currently have the following command:
find . -name "*\.[CH]" | grep -v /src/menu/
This gives me a list of all files that end in .C or .H but do not have
the string "/src/menu" in them.
This works, but I'm trying to eliminate the call to grep. I need a
regular expression that combines "*\.[CH]" with "-v /src/menu". I've
searched for examples on the web and usenet, but I still can't figure
it out. How do I do a logical-AND with regular expressions?
| |
| Chris F.A. Johnson 2004-04-21, 1:35 am |
| On Wed, 21 Apr 2004 at 00:11 GMT, Timur Tabi wrote:
> I currently have the following command:
>
> find . -name "*\.[CH]" | grep -v /src/menu/
>
> This gives me a list of all files that end in .C or .H but do not have
> the string "/src/menu" in them.
>
> This works, but I'm trying to eliminate the call to grep. I need a
> regular expression that combines "*\.[CH]" with "-v /src/menu". I've
> searched for examples on the web and usenet, but I still can't figure
> it out. How do I do a logical-AND with regular expressions?
If you wanted to eliminate by filename, it would be possible; find
matches only on the filename portion, not the full path.
--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
========================================
===========================
My code (if any) in this post is copyright 2004, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
| |
| Robert Bonomi 2004-04-21, 1:35 am |
| In article <53bb806a.0404201611.62515918@posting.google.com>,
Timur Tabi <nospam_timur@tabi.org> wrote:
>I currently have the following command:
>
>find . -name "*\.[CH]" | grep -v /src/menu/
>
>This gives me a list of all files that end in .C or .H but do not have
>the string "/src/menu" in them.
>
>This works, but I'm trying to eliminate the call to grep. I need a
>regular expression that combines "*\.[CH]" with "-v /src/menu". I've
>searched for examples on the web and usenet, but I still can't figure
>it out. How do I do a logical-AND with regular expressions?
*can't*, as asked.
What you want is simply:
find . (! -path '*/src/menu/*') -a -name '*[CH]' -print
| |
| Chris F.A. Johnson 2004-04-21, 1:35 am |
| On Wed, 21 Apr 2004 at 05:10 GMT, Robert Bonomi wrote:
> In article <53bb806a.0404201611.62515918@posting.google.com>,
> Timur Tabi <nospam_timur@tabi.org> wrote:
>
> *can't*, as asked.
>
> What you want is simply:
>
> find . (! -path '*/src/menu/*') -a -name '*[CH]' -print
If you need -print, you probably don't have the -path operator
(it's not part of the POSIX spec).
--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
========================================
===========================
My code (if any) in this post is copyright 2004, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
| |
| rakesh sharma 2004-04-21, 1:35 am |
| nospam_timur@tabi.org (Timur Tabi) wrote in message news:
>
> I currently have the following command:
>
> find . -name "*\.[CH]" | grep -v /src/menu/
>
> This gives me a list of all files that end in .C or .H but do not have
> the string "/src/menu" in them.
>
> This works, but I'm trying to eliminate the call to grep. I need a
> regular expression that combines "*\.[CH]" with "-v /src/menu". I've
> searched for examples on the web and usenet, but I still can't figure
> it out. How do I do a logical-AND with regular expressions?
>
if your 'find' has the -path option then you can try this:
find . ! -path /src/menu \( -name '*.[CH]' -o -name '.*.[CH]' \) -type f -print
Otherwise, you have to make do the -exec processing:
find . -type f \( -name '*.[CH]' -o -name '.*.[CH]' \) -exec sh -c '
case $1 in
*/src/menu*):;;
*) printf "%s\n" "$1";;
esac
' {} {} \;
| |
| Bill Marcum 2004-04-21, 2:34 am |
| On 20 Apr 2004 17:11:14 -0700, Timur Tabi
<nospam_timur@tabi.org> wrote:
> I currently have the following command:
>
> find . -name "*\.[CH]" | grep -v /src/menu/
>
> This gives me a list of all files that end in .C or .H but do not have
> the string "/src/menu" in them.
>
> This works, but I'm trying to eliminate the call to grep. I need a
> regular expression that combines "*\.[CH]" with "-v /src/menu". I've
> searched for examples on the web and usenet, but I still can't figure
> it out. How do I do a logical-AND with regular expressions?
With GNU find you can
find . -path "*/src/menu/" -prune -o -name "*.[CH]"
If your find doesn't support -path, you're probably stuck with grep.
--
Giraffe: a ruminant with a view.
| |
| Charles Demas 2004-04-21, 9:36 am |
| In article <ed24e4cf.0404202137.6603535b@posting.google.com>,
rakesh sharma <sharma__r@hotmail.com> wrote:
>nospam_timur@tabi.org (Timur Tabi) wrote in message news:
>
>
>if your 'find' has the -path option then you can try this:
>
>find . ! -path /src/menu \( -name '*.[CH]' -o -name '.*.[CH]' \) -type f -print
>
>Otherwise, you have to make do the -exec processing:
>
>find . -type f \( -name '*.[CH]' -o -name '.*.[CH]' \) -exec sh -c '
>case $1 in
> */src/menu*):;;
> *) printf "%s\n" "$1";;
>esac
>' {} {} \;
or you could exec this:
awk '/\/src\/menu/ {next} {print}'
Chuck Demas
--
Eat Healthy | _ _ | Nothing would be done at all,
Stay Fit | @ @ | If a man waited to do it so well,
Die Anyway | v | That no one could find fault with it.
demas@theworld.com | \___/ | http://world.std.com/~cpd
| |
| rakesh sharma 2004-04-21, 2:36 pm |
| demas@TheWorld.com (Charles Demas) wrote in message news:
>
[vbcol=seagreen]
> or you could exec this:
>
> awk '/\/src\/menu/ {next} {print}'
>
But what would the filename be in this case?
| |
| Charles Demas 2004-04-21, 5:34 pm |
| In article <ed24e4cf.0404211003.662cd8b4@posting.google.com>,
rakesh sharma <sharma__r@hotmail.com> wrote:
>demas@TheWorld.com (Charles Demas) wrote in message news:
>
>
>
>
>
>But what would the filename be in this case?
The filenames gathered by find.
or you could just pipe the output of the find statement
into the awk line shown.
Chuck Demas
--
Eat Healthy | _ _ | Nothing would be done at all,
Stay Fit | @ @ | If a man waited to do it so well,
Die Anyway | v | That no one could find fault with it.
demas@theworld.com | \___/ | http://world.std.com/~cpd
| |
| rakesh sharma 2004-04-22, 1:35 am |
| demas@TheWorld.com (Charles Demas) wrote in message news:
>
> The filenames gathered by find.
>
> or you could just pipe the output of the find statement
> into the awk line shown.
>
I think the OP wanted to know whether the string '/src/menu' is contained
within the filename and NOT within the file.
| |
| Brian Gough 2004-04-22, 5:34 am |
| nospam_timur@tabi.org (Timur Tabi) writes:
> This works, but I'm trying to eliminate the call to grep. I need a
> regular expression that combines "*\.[CH]" with "-v /src/menu". I've
> searched for examples on the web and usenet, but I still can't figure
> it out. How do I do a logical-AND with regular expressions?
The find command has "and", "or" and "not" options, which you can use
to combine different file patterns (or other tests). HTH.
--
Brian Gough
Network Theory Ltd,
Publishing the GNU Bash Reference Manual --- http://www.network-theory.co.uk/
| |
| Charles Demas 2004-04-22, 12:36 pm |
| In article <ed24e4cf.0404212055.6eecf569@posting.google.com>,
rakesh sharma <sharma__r@hotmail.com> wrote:
>demas@TheWorld.com (Charles Demas) wrote in message news:
>
>
>I think the OP wanted to know whether the string '/src/menu' is contained
>within the filename and NOT within the file.
and piping the output of find (filenames) into awk will
cause the filenames to be treated as input data to awk.
If you wanted awk to look into the files, they you'd do:
find (...) | xargs awk '(...)'
Chuck Demas
--
Eat Healthy | _ _ | Nothing would be done at all,
Stay Fit | @ @ | If a man waited to do it so well,
Die Anyway | v | That no one could find fault with it.
demas@theworld.com | \___/ | http://world.std.com/~cpd
|
|
|
|
|