taking the end off a path

I need a script to be able to take a path such as "/foo/bar/thing" a put the "/foo/bar/" bit in one variable and the "thing" bit in another.

I figured awk would probably be the best tool for the job but looking at the man page didn't seem to help. The only way i know how to use awk is with '{print $1, $2 ,$3}' but that doesn't help unless i can get it to count from the end of the string.

What should i do?

use basename to get the file name
and dirname to get the path.

program=/this/is/the/path/to/my/file.ksh

FILE=$(basename program)
DIR=$(dirname program)

echo $FILE
file.ksh

echo $DIR
/this/is/the/path/to/my

Dont use this code since its better to use built-ins rather than
writing something like this, but, here is a method in Awk to
get the parent directory of a file.


echo $PARENT_PATH | awk ' {
     
              arraySize = split($0, parentPath, "/");
              delete parentPath[arraySize];
    
              for (i in parentPath ) {
               w = w "/" parentPath;
  
              }
              print w
            }'

with a few minor changes it works great

thanks a lot :slight_smile:

what were the minor changes?

by minor changes i mean adapting it for my script and changing for example "FILE=$(basename program)" to "FILE=$(basename $program)"

I have also encountered another problem on closer investigation.

$0 which provides the input for dirname and basename does not put a \ before spaces which dirname especially doesn't like.

the only way around this that i can think of is to use something like;

no1=$($0 | awk '{print $1}')
no2=$($0 | awk '{print $2}')
no3=$($0 | awk '{print $3}')

execpath=$(echo $no1\ $no2\ $no3)

exedir=$(dirname $execpath)
exefile=$(filename $execpath)

however this would require me to know how many spaces exactly will be in the files path.

This is starting to look complex.
What do i do?

This is where you use quotes most liberally:

exedir=$(dirname "$0")
exefile=$(filename "$0")

Can you post some or all of your script? It would be easier to see the script to better trouble shoot it.

thanks to anarchie for the tip with double quotes.
Fixes this particular problem nicely.

Do you have another problem for the forum!!!? PS - Glad to help anyway I [we] can.

If you are using KSH try the following. Its really easy to use.

path=`print ${parent%/}`
filename=`print ${parent##
/}`

echo $path
/foo/bar

echo $filename
thing

In short the code for 'path' will print everything before the last /
The code for 'filename' will print eveything after the last /
It doesnt matter how many sub directories you have in the path name, it will always work.

Try it, Its probably one of my most used pieces of code!

Hope this helps
Helen

hi,

I would need a basename like action (or the '##*/') but probably in combination with awk.

I need it to transform ftp commandfiles from put to get syntax.

If this is the original file :
open 10.1.91.15
user caluwaek xxxxxxx
cd /export/home/caluwaek/ftp
put /export/home/caluwaek/ftp_bestanden/bestand.txt
put /export/home/caluwaek/ftp_bestanden/bestand2.txt bestand2_copy.txt
bye

I use sed to change put in get, as wel as to change the filenames from left to right. Result :
open 10.1.91.15
user caluwaek cindy75
cd /export/home/caluwaek/ftp
get /export/home/caluwaek/ftp_bestanden/bestand.txt
get bestand2_copy.txt /export/home/caluwaek/ftp_bestanden/bestand2.txt
bye

The only thing that remains to do is to drop the paths. Only on the lines which start with get and only if the file is on the right side.

Anybody some advice ?

Thanks!

Kris

If you are already using sed it is easier to do everything in sed (otherwise i would have suggested the variable expansion in the ksh too):

first:

/^get/ {
          ...command...
          }

issues commands only on lines starting with "get".

second:

\(\/[^<space>]*\/\)

is the regular expression to match anything between the first and the last "/" character in one "word" (1) and group it.

Hope this helps.

bakunin

(1) "a sequence of nonblank characters separated by blanks", according to an IBM OS/370 manual explaining what a "word" is, ROFL

$ cat file1
open 10.1.91.15
user caluwaek cindy75
cd /export/home/caluwaek/ftp
put /export/home/caluwaek/ftp_bestanden/bestand.txt
put /export/home/caluwaek/ftp_bestanden/bestand2.txt bestand2_copy.txt
bye

$ awk 'sub(/put/,"get",$1)&&$3{x=$2;$2=$3;$3=x};1' file1 > file2

$ cat file2
open 10.1.91.15
user caluwaek cindy75
cd /export/home/caluwaek/ftp
get /export/home/caluwaek/ftp_bestanden/bestand.txt
get bestand2_copy.txt /export/home/caluwaek/ftp_bestanden/bestand2.txt
bye