Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Breaking apart a variable 1

Status
Not open for further replies.

jestrada101

Technical User
Mar 28, 2003
332
What would be the best method to break apart a variable?

For example:
$> breakapart.ksh /export/home/user/myfile.txt


I want to be able to separate the directory structure from the filename. The directory structure can change, so it's not always going to be /export/home/user/

Thanks for any guidance.

JE
 
Try something like this:
Code:
myPathName="/export/home/user/myfile.txt"
echo "dirname=`dirname $myPathName`"
echo "filename=`basename $myPathName`"
If you're in ksh, you can do this:
Code:
echo "dirname=${myPathName%/*}"
echo "filename=${myPathName##*/"

Hope This Help
PH.
 
This is what I need.. but, if a directory is not specified for the directory, it'll show me the filename as the directory, is there a way to make it null or send some other value other than the filename:

myPathName="myfile.txt"
echo "dirname=${myPathName%/*}"
echo "filename=${myPathName##*/"

Thanks
JE

 
Hi,

You can user 'dirname ' and 'basename' commands :

myPathName="myfile.txt"
echo "dirname=$(dirname "$myPathName")" # -> .
echo "filename=$(basename "$myPathName")" # -> myfile.txt



Jean Pierre.
 
Have I missed the point ?
myPathName="/export/home/user/myfile.txt"
filename=$(basename $myPathName)

filename should then contain only 'myfile.txt'



Dickie Bird (:)-)))
 
Use the first suggestion...
[tt]
% echo "dirname=$(dirname $myPathName)"
dirname=.[/tt]
 
Thanks all.. I guess there wouldn't be a method to get null. I think this will work. Thanks again!

JE
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top