- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- a better way to exclude items other than a series ...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-10-2007 11:36 AM
04-10-2007 11:36 AM
I've got a list of file name extensions I don't want to see, and I want everything else from a "find" list.
e.g.
find . -type f | grep -v "\.xml" | grep -v "\.gif" | grep -v "\.htm" | grep -v "\.html"
...
and the list goes on for about 50 file extensions.
Can anyone please demonstrate a better way to exclude a list from a find than spawning a bazillion greps?
Note: handling it all in a single grep from the command line in the following fashion with something like "grep -e -v "\.gif" -e -v "\.htm" doesn't work.
Note2: I'd be find with putting all of the extensions in an input file of some sort, so that part doesn't have to come from the command line itself.
Thanks in advance
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-10-2007 11:50 AM
04-10-2007 11:50 AM
Solution1) Do it all with find:
# find . -type f \( ! -name "*.xml" -o ! -name "*.gif" -o ! -name "*.htm" -o ! -name "*.html" \)
Just keep listing your extensions. The '-o' means a 'logical or' and the '! -name "*.htm"' means NOT anything ending with .htm. Do a 'man find' for more info.
2) You could do it with find and grep:
# find . -type f | grep -v -E "\.xml|\.gif|\.htm|\.html"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-10-2007 12:20 PM
04-10-2007 12:20 PM
Re: a better way to exclude items other than a series of "grep -v" piped commands
Put the patterns you don't want returned in your selection in a file; something like:
# cat /tmp/excludes
.awk
.c
.htm
.html
.log
.old
.pl
.pm
.sh
Now do:
# find /tmp -type f |grep -E -v -f /tmp/excludes
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-10-2007 12:29 PM
04-10-2007 12:29 PM
Re: a better way to exclude items other than a series of "grep -v" piped commands
Oops! In your exclusive file, if you truly want to skip files with dot (".") suffixes, escape the dot in the exclusion specification. The file contains regular expressions and a dot signifies any character when not escaped. My example '/tmp/excludes' should have looked like:
# cat /tmp/excludes
\.awk
\.c
\.htm
\.html
\.log
\.old
\.pl
\.pm
\.sh
Regards!
...JRF...
- Tags:
- quoting
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2007 03:16 AM
04-11-2007 03:16 AM
Re: a better way to exclude items other than a series of "grep -v" piped commands
Thanks for the suggestions, I really like the ability to use the file with "grep -v -E -f" command -that exactly what I'm looking for.
Patrick - you're suggestion number #2 is quite cool, and I had BEEN wondering how to do an "or" in a grep for a word list *and* use it in an exclude grep function. I had only used it (and then rarely) before for include functions, and somehow never put 2+2 together to make it work for an exclude function. Thank you.
Upon reflection, the list method is going to win the day, because I want to account for all file extensions in the intended directory, and it will go into the hundreds of named extensions.
Thanks very much for the suggestions guys,
John
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2007 03:40 AM
04-11-2007 03:40 AM
Re: a better way to exclude items other than a series of "grep -v" piped commands
And to get that list of file name extensions going you might want to use:
perl -le 'while (<*>) { m/\.(.*)/; $seen{$1}++} print "\\.$_" foreach (sort keys %seen)'
Hein.
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2007 04:12 AM
04-11-2007 04:12 AM
Re: a better way to exclude items other than a series of "grep -v" piped commands
# find . -type f | grep -v "\.xml" | grep -v "\.gif" | grep -v "\.htm" | grep -v "\.html"
it's very ugly ;)
prefer a shortcut:
# touch foo.xml foo.txt foo.htm foo.html foo.avi foo.dat
# find . -type f |egrep -v "\.xml|\.gif|\.htm?"
./foo.txt
./foo.avi
./foo.dat
#
and better is:
# G_ARGS=".\xml|.gif|\.htm?"
# find . -type f |egrep -v "$G_ARGS"
./foo.txt
./foo.avi
./foo.dat
#
Regards,
Cedrick Gaillard
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2007 04:24 AM
04-11-2007 04:24 AM
Re: a better way to exclude items other than a series of "grep -v" piped commands
# cat /tmp/excludes
\.awk
\.c
\.htm
\.html
\.log
\.old
\.pl
\.pm
\.sh
You may want to "anchor" the end of line as well, esp if you want to exclude .htm but *not* .html's
something like:
\.htm$
\.log$
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2007 04:35 AM
04-11-2007 04:35 AM
Re: a better way to exclude items other than a series of "grep -v" piped commands
> OldSchool: You may want to "anchor" the end of line as well, esp if you want to exclude .htm but *not* .html's
Yes, absolutely true! I'm afraid that I was in too much of a hurry to catch a favorite television show and assumed the appropriate nuances applied. ;-)
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2007 05:40 AM
04-11-2007 05:40 AM
Re: a better way to exclude items other than a series of "grep -v" piped commands
Hein -> I finished a script to do this as the initial part of the problem, but I'm intrigued by your perl script - because I've been slowly learning perl. I'm betting I will be spending quite a bit of time trying to figure out how it works, thank you. I really hate to ask - BUT... would you mind posting a little synopsis on the parts of that script and how it works, so I could learn? If you don't have the time, or its too lengthy of a request, I certainly understand, so feel free to ignore the request. Thanks again all.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2007 06:38 AM
04-11-2007 06:38 AM
Re: a better way to exclude items other than a series of "grep -v" piped commands
It's actuallly no problem as I had it already written up two weeks ago.
And it is not perfect....
http://dcl.openvms.org/stories.php?story=07/03/22/8049141
There are more differences with the VMS version that I did not take care of.
- VMS needs to special case ";" as file version seperator
- and VMS typically is case-blind for filenames (Unless ODS-5 is used)
- VMS is strict about extentions allowing just 1 dot in the file name (unless ODS-5).
=> The ; provided a 'right side' anchor for the regexp. Need to use $ for "eol" under Unix.
=> Need to allow for multiple dots.
This gives:
$ perl -le 'while (<*>) {$seen{$1}++ if /\.([^.]*$)/} print "\\.$_" foreach (sort keys %seen)'
Explanation:
-l = print new line with each print
-e = program text to follow
while (<*>) { = loop over 'globbed' list of files, putting filename in automatic variable $_
$seen{uc($1)}++ = Increment (and create) an associative array elemement with name being last match from $1
if /\.([^.]*$)/ = Only do the aforementioned on a match of a piece of string to be called $1 with 'any non dot' after a period and before end of line. The first period is escaped with a backslash to make it real, not a wild character
} = loop end
print = print the default variable $_
foreach (sort = loop over sorted array from...
keys %seen = all the keys for associative array 'seen', stashed in default variable $_ one at a time.
Regards, Hein van den Heuvel
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2007 07:13 AM
04-11-2007 07:13 AM
Re: a better way to exclude items other than a series of "grep -v" piped commands
I was laughing to myself looking over the code aspects as you've written them; because had I written them in Perl, they would have come out being a) MUCH longer, and b) looking like someone who is used to writing in both C and ksh "wrote a combination C/ksh script in perl".
:-)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2007 03:55 PM
04-11-2007 03:55 PM
Re: a better way to exclude items other than a series of "grep -v" piped commands
Your only problem was you only need one -v and the -e must be right before each pattern.
And if you didn't want to quote those ".", use fgrep.
>Patrick: # find . -type f | grep -v -E "\.xml|\.gif|\.htm|\.html"
There is no reason to use the egrep hammer.
Just use -f for that file solution. Or use multiple -e:
grep -v -e "\.xml$" -e "\.gif$" ...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-12-2007 05:45 AM
04-12-2007 05:45 AM
Re: a better way to exclude items other than a series of "grep -v" piped commands
thanks for your response - however a bit of clarification here is needed. I said that the "grep -v -e" doesn't work, and it doesn't , at least in HPUX 11i (might work in the newer ones).
So, while the "grep -v -e" in repetition works fine in Linux, it does not on HPUX:
example:
$ cat > test
ehlllo
goodbye
bye
hello
no
yes
sayit
say
yell
scream
ice cream
ice
cream
$ cat test | grep -v -e "test" -v -e "yell" -v -e "ice"
ehlllo
goodbye
bye
hello
no
yes
sayit
say
yell
scream
ice cream
ice
cream
Notice that no lines are missing:
HOWEVER, on Linux the above test works as expected:
$ cat test | grep -v -e "test" -v -e "yell" -v -e "ice"
ehlllo
goodbye
bye
hello
no
yes
sayit
say
scream
cream
Which is why I put the question in the HPUX forum...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-12-2007 06:07 AM
04-12-2007 06:07 AM
Re: a better way to exclude items other than a series of "grep -v" piped commands
The '-v' switch needs to occur *once*:
# grep -v -e "test" -e "yell" -e "ice" file
By the way, you can skip the extra process (the 'cat') and let 'grep' open the file(s) specified as its argument(s) as above.
Regards!
...JRF...
- Tags:
- evil cat
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-12-2007 06:13 AM
04-12-2007 06:13 AM
Re: a better way to exclude items other than a series of "grep -v" piped commands
Actually, as noted above, it should be:
grep -v -e "\.gif" -e "\.html" .....
w/o repeating "-v".
Unfortunately, Linux isn't unix, its a work-alike, developed from observed behaviour / documentation of unix + "enhancements"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-12-2007 06:31 AM
04-12-2007 06:31 AM
Re: a better way to exclude items other than a series of "grep -v" piped commands
it looks like the Linux version has figured out how to ignore the repeatiing series of "-v" to make it work...
thanks all for the clarification,
and I see that re-reading Dennis' post, he didn't repeat the "-v" over and over again.
Sorry Dennis, wish they had a do-over button on the points.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-12-2007 10:38 AM
04-12-2007 10:38 AM
Re: a better way to exclude items other than a series of "grep -v" piped commands
I would assume you could repeat it and work, but I'm lazy. You just can't put the -v after the -e.
Ah, you're right, there is a bug in grep. They just increment vflag then they do bit stuff on it.
>Sorry Dennis, wish they had a do-over button on the points.
Ok, you can add the rest here. :-)
So you don't feel short changed, I filed a bug report on it:
CR JAGag37626:
Multiple -v in grep cause all to be ignored