LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to substring (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-substring-668544/)

santhoshv 09-09-2008 12:53 AM

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...

i92guboj 09-09-2008 01:03 AM

Quote:

Originally Posted by santhoshv (Post 3274116)
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}


Nylex 09-09-2008 01:03 AM

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!

colucix 09-09-2008 05:09 AM

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.


All times are GMT -5. The time now is 08:13 PM.