×
UNIX.COM Login
Username:
Password:  
Show Password






👤


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

👤 Login to reply

 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-20-2018
dampio dampio is offline
Registered User
 
Join Date: Dec 2016
Last Activity: 12 June 2018, 8:14 AM EDT
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 07:23 AM.. Reason: Added CODE tags.
Sponsored Links
    #2  
Old 01-20-2018
Scott Scott is offline Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 8 July 2018, 4:41 PM EDT
Posts: 9,062
Thanks: 402
Thanked 1,279 Times in 1,082 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 01-20-2018
dampio dampio is offline
Registered User
 
Join Date: Dec 2016
Last Activity: 12 June 2018, 8:14 AM EDT
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 07:10 AM..
    #4  
Old 01-20-2018
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 16 July 2018, 11:44 AM EDT
Location: Aachen, Germany
Posts: 13,060
Thanks: 447
Thanked 4,011 Times in 3,688 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 01-20-2018
Scott Scott is offline Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 8 July 2018, 4:41 PM EDT
Posts: 9,062
Thanks: 402
Thanked 1,279 Times in 1,082 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 01-23-2018
rbatte1 rbatte1 is offline Forum Staff  
Root armed
 
Join Date: Jun 2007
Last Activity: 16 July 2018, 10:44 AM EDT
Location: Lancashire, UK
Posts: 3,584
Thanks: 1,592
Thanked 706 Times in 633 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 (01-23-2018)
Sponsored Links
👤 Login to reply

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
print all filenames in directory with path to another file jacobs.smith Shell Programming and Scripting 1 06-07-2012 01:34 PM
Retrieve directory path from full file path through sh royzlife Shell Programming and Scripting 7 09-21-2010 08:28 AM
Print directory name along with their path jadoo_c2 Shell Programming and Scripting 5 07-19-2010 04:54 AM
Howto Print File Path or Print the Filename overkill Shell Programming and Scripting 1 10-09-2009 01:00 AM
How do I get awk to print a " in it's print part? LordJezo Shell Programming and Scripting 2 06-27-2006 09:16 PM



All times are GMT -4. The time now is 02:29 PM.

Unix & Linux Forums Content Copyrightę1993-2018. All Rights Reserved.