Getting the lines that contains a '/' N times(N given at runtime) | Unix Linux Forums | HP-UX

  Unix/Linux Go Back    


HP-UX HP-UX (Hewlett Packard UniX) is Hewlett-Packard's proprietary implementation of the Unix operating system, based on System V.

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

HP-UX


Tags
shell awk getline

Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 08-06-2013
bibou25 bibou25 is offline
Registered User
 
Join Date: Jun 2013
Last Activity: 6 August 2013, 12:39 PM EDT
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
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 :/ )

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):


Code:
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 :/ Any pointer to a more effective solution would be appreciated.

Matt
Sponsored Links
    #2  
Old Unix and Linux 08-06-2013
MadeInGermany MadeInGermany is offline Forum Advisor  
Registered User
 
Join Date: May 2012
Last Activity: 5 March 2015, 4:16 PM EST
Location: Simplicity
Posts: 2,235
Thanks: 153
Thanked 681 Times in 613 Posts
GNU find is fast and easy

Code:
find . -type d -mindepth 2 -maxdepth 2

Standard find (can be further optimized)

Code:
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:

Code:
awk -F/ NF==3


Last edited by MadeInGermany; 08-06-2013 at 01:33 PM..
The Following User Says Thank You to MadeInGermany For This Useful Post:
bibou25 (08-06-2013)
Sponsored Links
    #3  
Old Unix and Linux 08-06-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 5 March 2015, 2:34 PM EST
Location: Aachen, Germany
Posts: 5,436
Thanks: 94
Thanked 1,416 Times in 1,333 Posts
What about this one:
Code:
 find /usr/share -type d |awk '{n=gsub (/\//, "&")} n>10'

    #4  
Old Unix and Linux 08-06-2013
bibou25 bibou25 is offline
Registered User
 
Join Date: Jun 2013
Last Activity: 6 August 2013, 12:39 PM EDT
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
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 Unix or Linux Image

The gsub does not seem to do the trick :/

Anyway, the solution of MadeInGermany


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

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

Thanks all.
Sponsored Links
    #5  
Old Unix and Linux 08-06-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 5 March 2015, 2:34 PM EST
Location: Aachen, Germany
Posts: 5,436
Thanks: 94
Thanked 1,416 Times in 1,333 Posts
You of course have to adapt to your needs, e.g. n==3 or n==2...
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to print the lines which are repeated 3 times in a file? ailnilanjan Shell Programming and Scripting 3 05-05-2013 04:11 AM
How to copy set of lines n times? nsuresh316 UNIX for Dummies Questions & Answers 3 02-04-2013 03:16 AM
Extracting data between specific lines, multiple times captainalright UNIX for Dummies Questions & Answers 14 01-02-2013 04:29 PM
Sed or Awk for lines between two strings multiple times and keep the last one damanidada Shell Programming and Scripting 4 02-07-2012 06:11 AM
grep to give how many times each lines were found Browser_ice AIX 4 12-14-2006 09:37 AM



All times are GMT -4. The time now is 10:25 PM.