Extracting Details from string -- Help Me!!!

Hi Experts,

I am executing a script in bash profile and having file path as follows:

$FilePath=$HOST_NAME_LAND/home/chandan/abc.txt

where $HOST_NAME_LAND is an environment variable which points to the server name.

export $HOST_NAME_LAND=//<server name>

The environment variable name and server name may vary for different modules.

What I need is to separate the file path( /home/chandan/abc.txt ) from the environment variable( $HOST_NAME_LAND ), to check the existence of the file on that server?

Need your help!!

Thanks in advance.

Thanks,
Upayan

 echo $FilePath | cut -d"/" -f1

 echo "$FilePath" | sed   's/$HOST_NAME_LAND//'

Try:

[ -f ${FilePath#$HOST_NAME_LAND} ] && echo "File Exists"

Thanks Panayam and Chacko193.

This is not going to resolve the issue because the environment variable name, file name and file path will vary module wise. So, I need a more generic approach.

Thanks,
ChandanN

Try:

[ -f ${FilePath#//[a-zA-Z]*} ] && echo "File exists"

This assumes that the expansions of HOST_NAME_LAND and the other evn variables containing the server name starts with // (as per your OP) and they only contain alphabets.

If they contain numerals as well , try:

[ -f ${FilePath#//[a-zA-Z0-9]*} ] && echo "File exists"

Which language/shell are you using? the above is not a valid shell syntax.
Also, as others asked, if you want to be generic, please provide the criteria based on which the file name can be extracted.

Assuming your variable assignments did work, and that you are using a recent shell,

echo $FilePath
//<server name>/home/chandan/abc.txt
echo /${FilePath#//*/}
/home/chandan/abc.txt

should work.