Unix/Linux Go Back    


UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

A way to print only part of directory path

UNIX for Advanced & Expert Users


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-20-2018   -   Original Discussion by dampio
dampio's Unix or Linux Image
dampio dampio is offline
Registered User
 
Join Date: Dec 2016
Last Activity: 24 January 2018, 6:58 AM EST
Posts: 7
Thanks: 3
Thanked 1 Time in 1 Post
A way to print only part of directory path

Hi,

So I struggled to find a solution to the following problem:

I want to make sed print only part of multiple different paths.

So lets say we have



Code:
/path/path1/path2/logs/bla/blabla
/path/path1/path2/path3/logs/yadda/yadda/yadda

Can someone suggest a way to make sed or other utility print only the path name up to logs directory only? ie:



Code:
/path/path1/path2/logs/
/path/path1/path2/path3/logs

Thanks for the answers

By the way the solution will be implemented in a script which will run on multiple different machines, so the paths will always be different and not static.


Moderator's Comments:
A way to print only part of directory path Please use CODE tags as required by forum rules!

Last edited by RudiC; 01-20-2018 at 08:23 AM.. Reason: Added CODE tags.
Sponsored Links
    #2  
Old Unix and Linux 01-20-2018   -   Original Discussion by dampio
Scott's Unix or Linux Image
Scott Scott is offline Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 23 February 2018, 11:17 PM EST
Posts: 9,050
Thanks: 392
Thanked 1,274 Times in 1,078 Posts
Your desired output is inconsistent. You want the trailing /, or not?


Code:
sed "s|\(.*/logs\)[$/].*|\1|" # no
sed "s|\(.*/logs[$/]\).*|\1|" # yes

grep -o can do the same thing, probably more efficiently, but -o is possibly not portable, depending on where you plan to use it.
The Following User Says Thank You to Scott For This Useful Post:
dampio (01-20-2018)
Sponsored Links
    #3  
Old Unix and Linux 01-20-2018   -   Original Discussion by dampio
dampio's Unix or Linux Image
dampio dampio is offline
Registered User
 
Join Date: Dec 2016
Last Activity: 24 January 2018, 6:58 AM EST
Posts: 7
Thanks: 3
Thanked 1 Time in 1 Post
Quote:
Originally Posted by Scott View Post
Your desired output is inconsistent. You want the trailing /, or not?


Code:
sed "s|\(.*/logs\)[$/].*|\1|" # no
sed "s|\(.*/logs[$/]\).*|\1|" # yes

grep -o can do the same thing, probably more efficiently, but -o is possibly not portable, depending on where you plan to use it.

Thank you very much for the quick answer. It worked like a charm.

Edit: both suggestions work, I will see which I will use when doing the deep tests.
Edit2: Yes, I know about the grep, but I cannot use grep in my script logic. The directories get extracted via lsof or pfiles.

Last edited by dampio; 01-20-2018 at 08:10 AM..
    #4  
Old Unix and Linux 01-20-2018   -   Original Discussion by dampio
RudiC's Unix or Linux Image
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 24 February 2018, 3:46 PM EST
Location: Aachen, Germany
Posts: 12,174
Thanks: 372
Thanked 3,755 Times in 3,450 Posts
Please help me out - why can't you use grep when you can use sed to process commands' output? Did you consider shell's "parameter expansion / suffix removal"?
Sponsored Links
    #5  
Old Unix and Linux 01-20-2018   -   Original Discussion by dampio
Scott's Unix or Linux Image
Scott Scott is offline Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 23 February 2018, 11:17 PM EST
Posts: 9,050
Thanks: 392
Thanked 1,274 Times in 1,078 Posts
That didn't work so well, if logs was the last thing in the path.



Code:
sed -nE "s#(.*/logs(/|$)).*#\1#p"
sed -nE "s#(.*/logs)(/|$).*#\1#p"

Sponsored Links
    #6  
Old Unix and Linux 4 Weeks Ago   -   Original Discussion by dampio
rbatte1's Unix or Linux Image
rbatte1 rbatte1 is offline Forum Staff  
Root armed
 
Join Date: Jun 2007
Last Activity: 23 February 2018, 10:02 AM EST
Location: Lancashire, UK
Posts: 3,487
Thanks: 1,518
Thanked 679 Times in 612 Posts
How about a totally different route:-

Code:
find / -type d -name logs

Would that do what you need?



Robin
The Following User Says Thank You to rbatte1 For This Useful Post:
RudiC (4 Weeks Ago)
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
print all filenames in directory with path to another file jacobs.smith Shell Programming and Scripting 1 06-07-2012 02:34 PM
Retrieve directory path from full file path through sh royzlife Shell Programming and Scripting 7 09-21-2010 09:28 AM
Print directory name along with their path jadoo_c2 Shell Programming and Scripting 5 07-19-2010 05:54 AM
Howto Print File Path or Print the Filename overkill Shell Programming and Scripting 1 10-09-2009 02:00 AM
How do I get awk to print a " in it's print part? LordJezo Shell Programming and Scripting 2 06-27-2006 10:16 PM



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