Getting the lines that contains a '/' N times(N given at runtime)

Hi all,

The task I have sounds easy enough but my solution seems too much complicated and I would like some ideas/feedback on how to achieve the same goal with a more elegant solution on HP-UX B.11.23 (a grep with a regexp would be nice, I did not manage to do a working one :confused: )

Here is my problem, I have a list of dirname as input and I need to return only the ones that have the depth required (given at runtime)

How I have achieved this so far, please note that the "find . -type d" is just to give you a working example, input will be a flat file and nb_wanted will be set at runtime (here it is set to 2, +1 for the carriage return character):

find . -type d   | awk   -v nb_wanted=3 '{ whole_line=$0; cmd="echo \""$0"\" | sed \"s/[^\/]//g\" | wc -c " ; cmd|getline; nb_returned=$0; close(cmd); if (nb_returned==nb_wanted) print whole_line ; }'

This solution is pretty slow too :confused: Any pointer to a more effective solution would be appreciated.

Matt

GNU find is fast and easy

find . -type d -mindepth 2 -maxdepth 2

Standard find (can be further optimized)

find . -type d | awk -F"/" 'NF==3'

Since you have a file already, go for the awk solution! In this simple case you can omit the quotes:

awk -F/ NF==3
1 Like

What about this one:

 find /usr/share -type d |awk '{n=gsub (/\//, "&")} n>10'

I agree that GNU has the -maxdepth but as I said, input is a flat file and HP-UX's find does not support the -maxdepth option :slight_smile:

The gsub does not seem to do the trick :confused:

Anyway, the solution of MadeInGermany

find . -type d | awk -F"/" 'NF==3'

is an eye-candy compared to the mess I was using.

Thanks all.

You of course have to adapt to your needs, e.g. n==3 or n==2...