How do I exclude multiple directories in awk with find?
Hello :)
I found a script on webmaster world that mostly does what I need it to, but have been making modifications to tailor it to my specific needs. Here is the original command: Code:
find . -type f ¦ awk '!/\/\..*/ {dir=gensub(/(.+\/).+/,"\\1","g",$0); dir_list[dir]++} END {for (d in dir_list) printf "%s %s\n",d,dir_list[d]}' ¦ sort Code:
#dirwatch.sh Thanks! Damarr |
You can add multiple regexp concatenated by the logical AND, e.g.
Code:
awk '!/\/\..*/&&!/\/var/&&!/\/etc/{ blah blah blah }' Code:
find / \( -wholename /var -o -wholename /etc -o -wholename /tmp \) -prune -o -mindepth 2 -type f -print | awk blah blah blah |
Just thought I would point out some redundancies in your awk:
1. dir=gensub(/(.+\/).+/,"\\1","g",$0) - sub, gsub and gensub work on $0 unless otherwise specified, so it is not required here 2. dir_list[dir]++ - as dir is only used in this one spot you could easily combine this with the previous step, ie dir_list[gensub(/(.+\/).+/,"\\1","g")]++ 3. printf "%s %s\n",dir_list[d],d - two things on this one: a. as you have input a newline you may as well use print. b. by using print and because you have not changed the output field separator (OFS) you can use a comma to achieve the space like so - print dir_list[d],d All these suggestions are just that, suggestions :) |
Awesome!
Thanks for the help guys. Colucix, I ended up adding the && to the awk statement to exclude directories. Also found out how to put a space in, if the directory had a space in it... &&!/\/My\ Documents/ Grail, ended up incorporating all of your changes to neaten up the code. Thanks again :) -Damarr |
Sorry I was a bit sleepy when i looked at this and I do now have a suggestion for your actual question.
You should just be able to use the pipe alternator in your regex: Code:
find <blah> | awk '!/\/(..*|var|etc|My Documents)/{<blah>}' |
Grail,
Even though it works, I decided to try your new code out as it's cleaner, however I couldnt get it to work properly. -Damarr |
It doesn't find the directories or it finds the wrong ones?
|
It doesn't find any.
-Damarr EDIT: Got it, was missing a \ inside the paren Code:
awk '!/\/(\..*|var|temp)/ |
The solution suggested by grail should be
Code:
find <blah> | awk '!/\/(\.|var|etc|My Documents)/{<blah>}' Code:
/. OR /var OR /etc OR /My Documents |
The script has gone from 20 lines to 140 because of some different options I'm putting in (menu selection for output to shell, a log etc)
Since I don't want to have to change the search in each iteration I define it at the beginning Code:
searchstring='!/\/(\.|archive|my folder)/' Code:
awk $searchstring It did work when it was still part of the awk statement before I moved it up to define it only in one place... |
Double quotes should do the trick:
Code:
awk "$searchstring" |
Your suggestion worked, thanks a bunch :)
-damarr |
All times are GMT -5. The time now is 04:05 AM. |