LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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-09-2008, 01:53 AM   #1
santhoshv
LQ Newbie
 
Registered: Apr 2008
Posts: 8

Rep: Reputation: 0
Question How to substring


Hi,

I need a command to substring the passes content in the script file.

Ex if the passes input is
/local/data/temp/interface/DX7/sftpcubi/outbound/Test/file.txt

I need to extract only the directory path.

The value that is passed may be of any length but it is the combination of the directory path and the file name and from this input I only require to extract the file path and file name seperately.

Thanks
SaNv...
 
Old 09-09-2008, 02:03 AM   #2
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,063

Rep: Reputation: 381Reputation: 381Reputation: 381Reputation: 381
Quote:
Originally Posted by santhoshv View Post
Hi,

I need a command to substring the passes content in the script file.

Ex if the passes input is
/local/data/temp/interface/DX7/sftpcubi/outbound/Test/file.txt

I need to extract only the directory path.

The value that is passed may be of any length but it is the combination of the directory path and the file name and from this input I only require to extract the file path and file name seperately.

Thanks
SaNv...
In bash you could do:

Code:
PATH=/path/to/file
FILE=$(basename $PATH)
DIR=${PATH%$FILE}
 
Old 09-09-2008, 02:03 AM   #3
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
A simple way would be to search backwards through the string to find the first /, then take everything before as the directory and everything after as the filename. I'm not aware of a command that can do what you want (though perhaps someone else is), but a simple C++ program can do it the way I suggested above.

Edit: oh well, it seems there is a command!
 
Old 09-09-2008, 06:09 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
You can also use parameter substitution:
Code:
NAME=/path/to/some/testfile
FILE=${NAME##*/}
DIR=${NAME%/*}
These are equivalent to the basename and dirname commands, respectively.
 
  


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
seperating substring jkeertir Linux - Newbie 1 04-09-2008 01:37 AM
help regarding subString Replacement siva512 Linux - Newbie 2 04-03-2008 02:20 PM
substring in shell mystical dervish Programming 9 07-09-2007 09:22 PM
php preg_replace substring of a substring senyahnoj Programming 5 12-08-2006 12:31 PM
to get a substring dimah Programming 1 06-22-2006 07:31 AM


All times are GMT -5. The time now is 11:52 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