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

  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 Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 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 08-06-2013
MadeInGermany MadeInGermany is offline Forum Advisor  
Registered User
 
Join Date: May 2012
Last Activity: 20 December 2014, 11:11 AM EST
Location: Simplicity
Posts: 2,089
Thanks: 141
Thanked 613 Times in 556 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 08-06-2013
RudiC RudiC is online now Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 21 December 2014, 2:16 PM EST
Location: Aachen, Germany
Posts: 4,889
Thanks: 80
Thanked 1,224 Times in 1,150 Posts
What about this one:
Code:
 find /usr/share -type d |awk '{n=gsub (/\//, "&")} n>10'

    #4  
Old 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

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 08-06-2013
RudiC RudiC is online now Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 21 December 2014, 2:16 PM EST
Location: Aachen, Germany
Posts: 4,889
Thanks: 80
Thanked 1,224 Times in 1,150 Posts
You of course have to adapt to your needs, e.g. n==3 or n==2...
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 03:22 PM.