Computing.Net > Forums > Unix > Help parsing a path-filename

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Help parsing a path-filename

Reply to Message Icon

Name: csearle
Date: May 10, 2006 at 16:00:58 Pacific
OS: HP Unix 11.11
CPU/Ram: 16 GB
Product: HP
Comment:

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.pdf

thanks,
Chuck



Sponsored Link
Ads by Google

Response Number 1
Name: csearle
Date: May 10, 2006 at 16:11:12 Pacific
Reply:

More info:

If I use the following commands:

filename=/ctxfs/dm/350/junk.pdf
FTPDIR=`echo $filename | awk '{ print (match($filename,"/")) }'`
echo $FTPDIR

it returns the position of the FIRST matching slash - how can I get it to return the LAST matching slash?

Chuck


0

Response Number 2
Name: nails
Date: May 10, 2006 at 16:41:00 Pacific
Reply:

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 $d

f=$(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 x

v=${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.c

bn=$(my_basename $var .c)
echo $bn # displays: ttest

dn=$(my_dirname $var)
echo $dn # displays: /usr/nails/ddir


0

Response Number 3
Name: csearle
Date: May 10, 2006 at 17:08:23 Pacific
Reply:

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


0

Response Number 4
Name: ghostdog
Date: May 10, 2006 at 17:36:54 Pacific
Reply:

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


0

Response Number 5
Name: csearle
Date: May 10, 2006 at 18:14:34 Pacific
Reply:

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


0

Related Posts

See More



Response Number 6
Name: ghostdog
Date: May 10, 2006 at 21:22:14 Pacific
Reply:

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.


0

Response Number 7
Name: FishMonger
Date: May 10, 2006 at 21:33:48 Pacific
Reply:

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/ksh

pathname=/ctxfs/dm/350/junk.pdf

filedirectory=$(dirname $pathname)
echo $filedirectory

filename=$(basename $pathname)
echo $filename


0

Response Number 8
Name: FishMonger
Date: May 10, 2006 at 21:46:28 Pacific
Reply:

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


0

Response Number 9
Name: csearle
Date: May 10, 2006 at 22:33:45 Pacific
Reply:

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

regards,
Chuck


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Help parsing a path-filename

how to parse a path in unix www.computing.net/answers/unix/how-to-parse-a-path-in-unix/3301.html

Create a file with a path? www.computing.net/answers/unix/create-a-file-with-a-path/4820.html

parsing a variable to a function www.computing.net/answers/unix/parsing-a-variable-to-a-function-/5613.html