LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bash eliminate second last of the seperated string (https://www.linuxquestions.org/questions/linux-newbie-8/bash-eliminate-second-last-of-the-seperated-string-4175476595/)

ted_chou12 09-10-2013 08:29 AM

bash eliminate second last of the seperated string
 
Hi I would like to have:
/mnt/main/dir1/dir2/file.ext
to
/mnt/main/dir/

I have tried:
fname="${file#*/}"
and
bdir=$(echo "$file" | awk -F / '{if (NF>2) {print $NF}}')
seems like I am not getting the solution,
Thanks,
Ted.

schneidz 09-10-2013 08:36 AM

does this help:
Code:

[schneidz@hyper ~]$ d1=`dirname /mnt/main/dir1/dir2/file.ext`
[schneidz@hyper ~]$ dirname $d1
/mnt/main/dir1


Firerat 09-10-2013 08:41 AM

This will do it

Code:

file=/mnt/main/dir1/dir2/file.ext
echo "${file%/*/*}/"


ted_chou12 09-10-2013 12:22 PM

Thanks,
Ted


All times are GMT -5. The time now is 02:32 PM.