How to get the directory name from a path using csh?

Hi,

I want to get ABC and 924 from this path. How can i do so?
The length of the path can vary from but end will we same.

/home/abs/cad/bad_BAD/vdhingra/testcases/ABC/924/work

Similarly, CBA and 234 from this path.
/home/abs/cad/aaa/bad_BAD/vdhingra/testcases/CBA/234/work

$ echo "/home/abs/cad/bad_BAD/vdhingra/testcases/ABC/924/work" | awk -F "/" '{print $(NF-1)}'
924

$ echo "/home/abs/cad/bad_BAD/vdhingra/testcases/ABC/924/work" | awk -F "/" '{print $(NF-2)}'
ABC

Guru

1 Like
 
echo  $path | awk -F\/ '{print $(NF-2), $(NF-1)}'
1 Like
# set a="/home/abs/cad/bad_BAD/vdhingra/testcases/ABC/924/work"
# echo $a|sed -r 's/.*\/([^/]*)\/[^/]*\/.*/\1/'
ABC
# echo $a|sed -r 's/.*\/([^/]*)\/.*/\1/'
924
1 Like

hi,
Can you explane me how this command is working?

echo  $path | awk -F\/ '{print $(NF-2), $(NF-1)}'

How can i store them in two different variables?

NF is the mean "The Number of Fields" in the current input record.
check this..

# echo $pathx|awk -F"/" '{print NF}'
10
total fields count is 10

So while your FS --> '/'
your $2--> home
your $5--> bad_BAD
$NF = $10 --> work (last element)

# set pathx="/home/abs/cad/bad_BAD/vdhingra/testcases/ABC/924/work"
set b=`echo $pathx | awk -F\/ '{print $(NF-2)}'`  # 8.the element
set c=`echo $pathx | awk -F\/ '{print $(NF-1)}'`   # 9.the element
# echo $b $c
ABC 924
1 Like