Sponsored Content
Top Forums UNIX for Advanced & Expert Users A way to print only part of directory path Post 303011582 by dampio on Saturday 20th of January 2018 06:43:39 AM
Old 01-20-2018
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:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 01-20-2018 at 08:23 AM.. Reason: Added CODE tags.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

getting the path part of an argument

Seems I'm inundating this forum with questions, but anyway: I am writing a script that should accept one and only one argument when called. That argument should designate a file, either with path/filename or just filename. Now to the difficult bit: I want to figure out a way to store... (9 Replies)
Discussion started by: ropers
9 Replies

2. Shell Programming and Scripting

How do I get awk to print a " in it's print part?

The line is simple, use " '{ print $1"]"$2"\"$3THE " NEEDS TO GO HERE$4 }' I've tried \", "\, ^" and '"" but none of it works. What am I missing? Putting in the [ between $1 and $2 works fine, I just need to do the same with a ". Thanks. (2 Replies)
Discussion started by: LordJezo
2 Replies

3. Shell Programming and Scripting

retrieve part of file path

Hi I am trying to use sed to retrieve part of my html file's path. I am having a hard time getting what I want. Could someone give me some help? I want to retrieve the section after html and before the file name For example if I have the following, ... (3 Replies)
Discussion started by: tiger66
3 Replies

4. Shell Programming and Scripting

Howto Print File Path or Print the Filename

I'm trying to clean up my samba share and need to print the found file or print the path of the image it tried to searched for. So far I have this but can't seem to get the logic right. Can anyone help point me in the right direction? for FILE in `cat list`; do if ; then ... (1 Reply)
Discussion started by: overkill
1 Replies

5. Shell Programming and Scripting

Print directory name along with their path

Can any one tell me that how can i print all directory with their path in a given parent directory. i.e. parent directory /home/usr/ Now this shoe directory may contain sevral directory /home/usr dir1/ dir1.1/ dir1.2/ dir2 dir2.1/ dir2.2/ ... (5 Replies)
Discussion started by: jadoo_c2
5 Replies

6. Shell Programming and Scripting

Retrieve directory path from full file path through sh

Hi, I have a file abcd.txt which has contents in the form of full path file names i.e. $home> vi abcd.txt /a/b/c/r1.txt /q/w/e/r2.txt /z/x/c/r3.txt Now I want to retrieve only the directory path name for each row i.e /a/b/c/ /q/w/e/ How to get the same through shell script?... (7 Replies)
Discussion started by: royzlife
7 Replies

7. Shell Programming and Scripting

print all filenames in directory with path to another file

hi, i have a directory at /path/unix with the following files 1.txt 2.txt 3.txt 4.txt I want to make another file called filenames.txt at a different location called /path/home. So, my output file would be /path/home/filenames.txt with contents /path/unix/1.txt... (1 Reply)
Discussion started by: jacobs.smith
1 Replies

8. UNIX for Dummies Questions & Answers

Extract directory name from the full directory path in UNIX using shell scripting

My input is as below : /splunk/scrubbed/rebate/IFIND.REBTE.WROC.txt /splunk/scrubbed/rebate/IFIND.REBTE.WROC.txt /splunk/scrubbed/loyal/IFIND.HELLO.WROC.txt /splunk/scrubbed/triumph/ifind.triumph.txt From the above input I want to extract the file names only . Basically I want to... (5 Replies)
Discussion started by: IshuGupta
5 Replies

9. UNIX for Beginners Questions & Answers

Convert Relative path to Absolute path, without changing directory to the file location.

Hello, I am creating a file with all the source folders included in my git branch, when i grep for the used source, i found source included as relative path instead of absolute path, how can convert relative path to absolute path without changing directory to that folder and using readlink -f ? ... (4 Replies)
Discussion started by: Sekhar419
4 Replies

10. UNIX for Beginners Questions & Answers

What is the difference ../directory path and ./directory path in ksh?

What is the difference ../directory path and ./directory path in ksh? (1 Reply)
Discussion started by: TestKing
1 Replies
EXCHANGEDATA(2) 					      BSD System Calls Manual						   EXCHANGEDATA(2)

NAME
exchangedata -- atomically exchange data between two files SYNOPSIS
#include <unistd.h> #include <sys/attr.h> int exchangedata(const char * path1, const char * path2, unsigned int options); DESCRIPTION
The exchangedata() function swaps the contents of the files referenced by path1 and path2 in an atomic fashion. That is, all concurrent pro- cesses will either see the pre-exchanged state or the post-exchanged state; they can never see the files in an inconsistent state. The data in all forks is swapped in this way. The options parameter lets you control specific aspects of the function's behaviour. Open file descriptors follow the swapped data. Thus, a descriptor that previously referenced path1 will now reference the data that's acces- sible via path2, and vice versa. In general, the file attributes (metadata) are not exchanged. Specifically, the object identifier attributes (that is, the ATTR_CMN_OBJID and ATTR_CMN_OBJPERMANENTID attributes as defined by the getattrlist(2) function) are not swapped. An exception to this general rule is that the modification time attribute ( ATTR_CMN_MODTIME ) is swapped. When combined, these features allow you to implement a 'safe save' function that does not break references to the file (for example, aliases). You first save the new contents to a temporary file and then exchange the data of the original file and the temporary. Programs that reference the file via an object identifier will continue to reference the original file, but now it has the new data. The path1 and path2 parameters must both reference valid files. All directories listed in the path names leading to these files must be searchable. You must have write access to the files. The options parameter is a bit set that controls the behaviour of exchangedata(). The following option bits are defined. FSOPT_NOFOLLOW If this bit is set, exchangedata() will not follow a symlink if it occurs as the last component of path1 or path2. RETURN VALUES
Upon successful completion a value of 0 is returned. Otherwise, a value of -1 is returned and errno is set to indicate the error. COMPATIBILITY
Not all volumes support exchangedata(). You can test whether a volume supports exchangedata() by using getattrlist(2) to get the volume capabilities attribute ATTR_VOL_CAPABILITIES, and then testing the VOL_CAP_INT_EXCHANGEDATA flag. ERRORS
exchangedata() will fail if: [ENOTSUP] The volume does not support exchangedata(). [ENOTDIR] A component of the path prefix is not a directory. [ENAMETOOLONG] A component of a path name exceeded NAME_MAX characters, or an entire path name exceeded PATH_MAX characters. [ENOENT] Either file does not exist. [EACCES] Search permission is denied for a component of the path prefix. [ELOOP] Too many symbolic links were encountered in translating the pathname. [EFAULT] path1 or path2 points to an invalid address. [EXDEV] path1 and path2 are on different volumes (mounted file systems). [EINVAL] path1 or path2 reference the same file. [EINVAL] You try to exchange something other than a regular file (for example, a directory). [EIO] An I/O error occurred while reading from or writing to the file system. SEE ALSO
getattrlist(2) HISTORY
A exchangedata() function call appeared in Darwin 1.3.1 (Mac OS X version 10.0). Darwin December 15, 2003 Darwin
All times are GMT -4. The time now is 05:02 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy