LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-10-2013, 09:29 AM   #1
ted_chou12
Member
 
Registered: Aug 2010
Location: Zhongli, Taoyuan
Distribution: slackware, windows, debian (armv4l GNU/Linux)
Posts: 421
Blog Entries: 28

Rep: Reputation: 2
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.
 
Old 09-10-2013, 09:36 AM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,027

Rep: Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845
does this help:
Code:
[schneidz@hyper ~]$ d1=`dirname /mnt/main/dir1/dir2/file.ext`
[schneidz@hyper ~]$ dirname $d1
/mnt/main/dir1
 
1 members found this post helpful.
Old 09-10-2013, 09:41 AM   #3
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
This will do it

Code:
file=/mnt/main/dir1/dir2/file.ext
echo "${file%/*/*}/"
 
1 members found this post helpful.
Old 09-10-2013, 01:22 PM   #4
ted_chou12
Member
 
Registered: Aug 2010
Location: Zhongli, Taoyuan
Distribution: slackware, windows, debian (armv4l GNU/Linux)
Posts: 421
Blog Entries: 28

Original Poster
Rep: Reputation: 2
Thanks,
Ted
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] regex help for string seperated by forwardslashes szboardstretcher Programming 3 05-10-2013 12:22 AM
[SOLVED] How to escape bash-special character in a bash string? fantasy1215 Programming 12 03-10-2012 10:45 AM
replace string by another string in bash shell chinhth2010 Linux - General 2 08-02-2011 11:40 PM
Bash String help Fredstar Programming 1 09-03-2008 01:58 PM
Bash way to tell if String is in String tongar Programming 3 06-16-2005 07:59 AM


All times are GMT -5. The time now is 12:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration