awk question

Hello Peeps,

How can I use awk to strip the filename off the end please ?

/dir/dir/dir/dir/filename ?

Thanks,
Dave

echo '/dir/dir/dir/dir/filename' | awk -F/ -v OFS=/ '{NF--;$1=$1;print}'

i would do this instead. translate the / to a space. then awk the 5th field.

tr "/" " " ./file | awk '{print $5}'

why do you need a 'tr'?

tr means translate. It's a cool function, you can translate most things from one character to another. It works like sed more or less, just one more tool to have in your unix bag of tricks.

ah, i see - thanks.
but why do you need it here exactly?

Is basename a HP-UX command only? Can it be used across different flavors?

tr is like awk, sed, any other unix command. tr indicates you want to begin "translating" something to follow. What you want to translate, followed by what you want it to become, then the filename, unless your piping it. I think you can do "man tr" at a unix prompt and get the info on it.

cheers

hey buddy we know about tr, but the point is why use it if we can get the same result from awk alone.

Hi Vergsh99, I tried it but this is what I got ..

appdba01@dbsdev15 $ echo '/home/dba/appdba01/clone.log' | awk -F/ -v FS=/ '{NF--;$1=$1;print}'
awk: syntax error near line 1
awk: bailing out near line 1

not sure about about HP-UX, but most likely.

if on Solaris, use 'nawk' instead of 'awk'

I am on solaris. Thanks for your help. Now I get this ..

appdba01@dbsdev15 $ echo '/home/dba/appdba01/clone.log' | nawk -F/ -v S=/ '{NF--;$1=$1;print}'
home dba appdba01

pls see my original posting and compare it with what you're doing.

It doesn't matter. Personal preference i guess. To me the awk command is a bit complicated. It's not something you could easily come up with, unless you know it. Whereas the tr command is a bit easier to follow.

sorry, dont get it

mine:

echo '/dir/dir/dir/dir/filename' | awk -F/ -v OFS=/ '{NF--;$1=$1;print}'

yours:

echo '/home/dba/appdba01/clone.log' | nawk -F/ -v S=/ '{NF--;$1=$1;print}'

what IS the difference?

...and if the path is SIX directories deep?
...and if one of the directories have a SPACE in its name?

appprj03@dbsdev15 $ echo '/home/dba/appdba01/clone.log' | nawk -F/ -v OFS=/ '{NF--;$1=$1;print}'
/home/dba/appdba01

wanted clone.log

thanks.

that's what 'stripping off' means.

/home/dba/appdba01/clone.log' | nawk -F/ '{print $NF}'