Operating System - HP-UX
1833692 Members
4763 Online
110062 Solutions
New Discussion

Re: Need the filename in an absolute path

 
SOLVED
Go to solution
Hunki
Super Advisor

Need the filename in an absolute path


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.
12 REPLIES 12
Steven Schweda
Honored Contributor
Solution

Re: Need the filename in an absolute path

"man basename" or think harder with "sed".
Sandman!
Honored Contributor

Re: Need the filename in an absolute path

# find -type f -exec basename {} \;
Steven Schweda
Honored Contributor

Re: Need the filename in an absolute path

For example (using "sed"):

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.)
James R. Ferguson
Acclaimed Contributor

Re: Need the filename in an absolute path

Hi Hunki:

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...
Hunki
Super Advisor

Re: Need the filename in an absolute path


Hi Steve,

Could you explain a little bit about sed -e 's|.*/||' ..

Thanks.
spex
Honored Contributor

Re: Need the filename in an absolute path

> sed -e 's|.*/||'

Alternate delimiter "|" is used.

s| = substitute
.*/ = all characters leading up to a final "/" (greedy)
|| = with nothing
Sandman!
Honored Contributor

Re: Need the filename in an absolute path

>Could you explain a little bit about sed -e 's|.*/||' ..

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.
Hunki
Super Advisor

Re: Need the filename in an absolute path

Thanks Sandman , what is the difference between / and |
Sandman!
Honored Contributor

Re: Need the filename in an absolute path

Difference between "/" and "|" is that if the slash is used as the delimiter then the presence of "/" characters in the replacement or substitution pattern of the sed contruct would have to be escaped i.e.

# 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/.*\///'
James R. Ferguson
Acclaimed Contributor

Re: Need the filename in an absolute path

Hi Hunki:

> 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...
Steven Schweda
Honored Contributor

Re: Need the filename in an absolute path

And, if I had done what I had intended to do,
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
Hunki
Super Advisor

Re: Need the filename in an absolute path

Thanks to ALL for a wonderful knowledgeable explanations.