- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Need the filename in an absolute path
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-21-2007 05:09 AM
06-21-2007 05:09 AM
I have a list of filenames ( from the find command ) in this fashion :
/a/b/c/d.xml
/a/b/c/d/e/f.xml
What I need out of this list is the last field everytime ( i.e. d.xml and f.xml ) but the problem is that the directory hierarchy is not consistant ( I mean the depth changes with almost every file ) . How to overcome this problem.
Thanks.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-21-2007 05:16 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-21-2007 05:24 AM
06-21-2007 05:24 AM
Re: Need the filename in an absolute path
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-21-2007 05:27 AM
06-21-2007 05:27 AM
Re: Need the filename in an absolute path
td176> echo '/a/b/c/d.xml' | sed -e 's|.*/||'
d.xml
td176> echo '/a/b/c/d/e/f.xml' | sed -e 's|.*/||'
f.xml
If you don't trust ".*" to grab as much as
it can, you could look for non-slash
characters only:
td176> echo '/a/b/c/d.xml' | sed -e 's|.*/\([^/]*\)|\1|'
d.xml
As usual, there's more than one way to solve
a problem like this. (And "basename" is the
easiest one.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-21-2007 05:42 AM
06-21-2007 05:42 AM
Re: Need the filename in an absolute path
I would urge you to look at using 'basename'. The manpages offer additional details. Along with extracting the last part of a path (the filename or basename), you can optionally drop a suffix, too:
# F=/a/b/c/code.o
# basename ${F}
code.o
# basename ${F} ".o"
code
A companion function to 'basename' is 'dirname' which returns the directory in which the base(name) resides:
# F=/a/b/c/code.o
# /a/b/c
Yet another alternative is to roll-your-own using the shell parameter substitution:
# F=/a/b/c/code.o
# echo ${F##*/}
code.o
# F=/a/b/c/code.o
# echo ${F%/*}
/a/b/c
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2007 02:01 AM
06-22-2007 02:01 AM
Re: Need the filename in an absolute path
Hi Steve,
Could you explain a little bit about sed -e 's|.*/||' ..
Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2007 02:14 AM
06-22-2007 02:14 AM
Re: Need the filename in an absolute path
Alternate delimiter "|" is used.
s| = substitute
.*/ = all characters leading up to a final "/" (greedy)
|| = with nothing
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2007 02:44 AM
06-22-2007 02:44 AM
Re: Need the filename in an absolute path
The sed cmd strips all directories in the file pathname leaving only the name of the file. Note that the sed cmd separator chosen is the vertical bar "|" different from the default of slash "/" in order make the string to be parsed unambiguous to sed.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2007 03:15 AM
06-22-2007 03:15 AM
Re: Need the filename in an absolute path
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2007 03:32 AM
06-22-2007 03:32 AM
Re: Need the filename in an absolute path
# sed -e 's|.*/||'
the slash in the construct above does not need to be escaped but needs to be escaped in the construct below in order to keep things unambiguous to sed.
# sed -e 's/.*\///'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2007 03:33 AM
06-22-2007 03:33 AM
Re: Need the filename in an absolute path
> what is the difference between / and |
The principal difference is avoiding the "leaning toothpick syndrome" and thereby making the expression more readable. That is, using "|" in lieu of "/" means that we don't have to escape the delimiter to distinguish it from a path character in this case. Consider:
# sed -e 's|.*/||'
...would have to be written as:
# sed -e 's/.*\///'
...which makes the expression less unreadable in the second case.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2007 06:24 AM
06-22-2007 06:24 AM
Re: Need the filename in an absolute path
there would have been a "$" in there to tie
the extracted part to the end of the line.
For example:
sed -e 's|.*/\([^/]*$\)|\1|'
instead of:
sed -e 's|.*/\([^/]*\)|\1|'
And, while everyone normally uses "/", with
the occasional "|", you try to can confuse
the reader by using pretty much any character
you'd like:
td176> echo '/a/b/c/d.xml' | sed -e 'sH.*/\([^/]*$\)H\1H'
d.xml
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2007 08:14 AM
06-22-2007 08:14 AM