awk / escape character

Hi
I'm trying to split a dir listing
eg
/home/foo1/foo2
I'm using ksh

I've tried
dir=/home/foo1/foo2
splitit=`echo $dir | awk -F '\/' '{print $1}'`
echo $splitit

nothing is output!
I have checked the escape character. The only one I have found is \

BTW `pwd` | awk -F \/ '{print $1}' works from cmdline

Why is this not working?

I want to split the string and then put it into an array, get the size of it and then get the right most directory

no need to 'escape'.
Given a sample input, if you need to echo 'foo2':

dir=/home/foo1/foo2
splitit=`echo $dir | awk -F '/' '{print $NF}'`
echo $splitit

the same could be accomplished easier using 'basename' - 'man basename'

thank you for reply vgersh99 I am on Linux but '/' returns blank in inline awk

I look at basename, but is it only for directory name that you know already as argument passed?

thanks

I'm not following what's being said here - could you provide a sample input AND a desired output, pls?

Sorry
I have used basename foo2 and it returns foo2

I did not see the NF on the end - now I get 6 (yes 6!) returned to this script
dir=/home/foo1/foo2
splitit=`echo $dir | awk -F '/' '{print $NF}'`
echo $splitit

when i use the script below blank is returned
dir=/home/foo1/foo2
splitit=`echo $dir | awk -F '/' '{print $1}'`
echo $splitit

desired output is foo2.
last folder name is only known when the script is run (its run from another script)

thanks

Hm...... I don't see where '6' would be returned...

a 'blank' is what I would expect.
you're outputting '$1' - FIRST field. You have an ABSOLUTE path with the LEADING '/'.
What would be the FIRST field in a string '/a/b/c/d'?
It would be the string BEFORE the FIRST fieldSeparator. What is it in this case? It's the 'blank' character.
field1 -> 'blank'
field2 -> a
field3 -> b
field4 -> c
field5 -> d

yes my print field separator number was wrong :slight_smile: thanks for explaining it well
I will now put the values in an array, get size of array and from last array value get name of last directory.
I think this is an over complicated way.
when I manned basename it seemed that you must give filename - this is unknown in my script

why? what is an array and why do you need a 'size of it'?
awk's 'NF' will give you the 'Number of Fields' [NF] of the crrect record already.
Using my previously posted solution using '$NF' should give you the value of the LAST field.

why are you saying that?
'basename' can take any PATH - be it path to a file OR a path to a directory.

i did not understand what NF was, now i do

NF will solve my problem
thank you vgersh99 you have been very helpful