Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Dear gurus, (from a Unix beginner)
After reading man pages on awk/sed, and still not having a clue, would someone show me the way in a Unix script to parse a filename into parts:
pathname=/ctxfs/dm/350/junk.pdf
and resulting in the values
filedirectory=/ctxfs/dm/350
filename=junk.pdfthanks,
Chuck

More info:
If I use the following commands:
filename=/ctxfs/dm/350/junk.pdf
FTPDIR=`echo $filename | awk '{ print (match($filename,"/")) }'`
echo $FTPDIRit returns the position of the FIRST matching slash - how can I get it to return the LAST matching slash?
Chuck

Chuck:
There are two unix commands, basename, and dirname that will do what you want or you can create your own shell versions of the commands:
#!/bin/ksh
var=/home/nails/ddir/test.c
d=$(dirname $var)
echo $df=$(basename $var)
echo $f# Using ksh pattern-matching operators, this function
# replaces the unix basename command. If a 2nd argument
# exists, strip off the extension.
function my_basename {
typeset v xv=${1##*/}
x=${2#.} # get rid of the '.'
v=${v%.$x}
echo $v
}
# Using ksh pattern-matching operators, this function
# replaces the unix dirname command.
function my_dirname {
typeset v="$@"echo ${v%/*}
}# basename and dirname function examples
bn=$(my_basename $var)
echo $bn # displays: ttest.cbn=$(my_basename $var .c)
echo $bn # displays: ttestdn=$(my_dirname $var)
echo $dn # displays: /usr/nails/ddir

Thanks, Nails.
The filenames I have to parse are actually retrieved from a database call, and are not actual files on the Unix system, so I don't think the basename / dirname will be the solution.
I think I am just needing a simple way to parse that string.
regards,
Chuck

if you have python in your unix , you can do this:
import os
thefilepath = "/ctxfs/dm/350/junk.pdf"
path,fi = os.path.split(thefilepath)
print "The path is ", path
print "The file is ", fi

Thanks, ghostdog.
I'd be lying if I said I knew what python was, but I'll go look it up.
Is there an easy way to do this with the standard awk, sed, etc?
regards,
Chuck

hi
i have not been using awk,sed for tasks like this ever since i started using Python. Its just another scripting language with OO capabilities. I use it for my daily unix admin tasks, as well as writing programs for running in cron. There should be an easy way in awk or sed though, just have to wait for the gurus to reply. Sorry for not being of much help.

Why would you think that basename and dirname won't work? Did you try it?
Your database call needs to assign the filename to a variable. Just apply the basename and dirname to the variable like nails showed.
#!/bin/kshpathname=/ctxfs/dm/350/junk.pdf
filedirectory=$(dirname $pathname)
echo $filedirectoryfilename=$(basename $pathname)
echo $filename

If you want another option, you cane use a Perl script.
#!/usr/bin/perl
use File::Basename;
$file = '/ctxfs/dm/350/junk.pdf';
print dirname($file),$/;
print basename($file);
outputs:
/ctxfs/dm/350
junk.pdf

The basename and dirname commands did the trick! Thanks for all of your support - it is appreciated.
regards,
Chuck

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |