Extracting Details from string -- Help Me!!!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extracting Details from string -- Help Me!!!
# 1  
Old 07-24-2014
Question Extracting Details from string -- Help Me!!!

Hi Experts,

I am executing a script in bash profile and having file path as follows:
Code:
$FilePath=$HOST_NAME_LAND/home/chandan/abc.txt

where $HOST_NAME_LAND is an environment variable which points to the server name.
Code:
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

Last edited by Don Cragun; 07-24-2014 at 04:26 AM.. Reason: Add CODE and ICODE tags.
# 2  
Old 07-24-2014
Code:
 echo $FilePath | cut -d"/" -f1

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

# 3  
Old 07-24-2014
Try:
Code:
[ -f ${FilePath#$HOST_NAME_LAND} ] && echo "File Exists"

# 4  
Old 07-24-2014
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
# 5  
Old 07-24-2014
Try:
Code:
[ -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:
Code:
[ -f ${FilePath#//[a-zA-Z0-9]*} ] && echo "File exists"

# 6  
Old 07-24-2014
Quote:
Originally Posted by ChandanN
Hi Experts,
I am executing a script in bash profile and having file path as follows:
Code:
$FilePath=$HOST_NAME_LAND/home/chandan/abc.txt

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.
# 7  
Old 07-24-2014
Assuming your variable assignments did work, and that you are using a recent shell,
Code:
echo $FilePath
//<server name>/home/chandan/abc.txt
echo /${FilePath#//*/}
/home/chandan/abc.txt

should work.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extracting substring within string between 2 token within the string

Hello. First best wishes for everybody. here is the input file ("$INPUT1") contents : BASH_FUNC_message_begin_script%%=() { local -a L_ARRAY; BASH_FUNC_message_debug%%=() { local -a L_ARRAY; BASH_FUNC_message_end_script%%=() { local -a L_ARRAY; BASH_FUNC_message_error%%=() { local... (3 Replies)
Discussion started by: jcdole
3 Replies

2. UNIX for Dummies Questions & Answers

at -l doesnt give details of the scheduled job. How to get the details?

I have scheduled couple of shell scripts to run using 'at' command. The o/p of at -l is: $ at -l 1320904800.a Thu Nov 10 01:00:00 2011 1320894000.a Wed Nov 9 22:00:00 2011 1320876000.a Wed Nov 9 17:00:00 2011 $ uname -a SunOS dc2prcrptetl2 5.9 Generic_122300-54 sun4u sparc... (2 Replies)
Discussion started by: superparticle
2 Replies

3. UNIX for Dummies Questions & Answers

Extracting numbers from a String

Hi all, I'm a new programmer to shell script... and I have no idea how to use substring. I want to extract the numbers from the following string and place it into a variable: "170 unique conformations found" The numbers can be more than three digits depending on the case. I just want to... (10 Replies)
Discussion started by: ah7391
10 Replies

4. Shell Programming and Scripting

Extracting particular string in a file and storing matched string in output file

Hi , I have input file and i want to extract below strings <msisdn xmlns="">0492001956</ msisdn> => numaber inside brackets <resCode>3000</resCode> => 3000 needs to be extracted <resMessage>Request time getBalances_PSM.c(37): d out</resMessage></ns2:getBalancesResponse> => the word... (14 Replies)
Discussion started by: sushmab82
14 Replies

5. Shell Programming and Scripting

Extracting Date from string

Hi Gurus I want to extract a date and version code which shall come in filename consisting of underscores. The filename can contain any / one underscores but the version number will come after date and will be separted by underscore String formats ============= ABC_20090815_2.csv... (13 Replies)
Discussion started by: r_t_1601
13 Replies

6. Shell Programming and Scripting

Extracting String

I am trying to extract a hyperlink from a html document using awk. I have managed to output in the format... href="index.html"> where i would like it just to output index.html. Any ideas on how i would do this? Thanks (2 Replies)
Discussion started by: adpe
2 Replies

7. Shell Programming and Scripting

extracting a string

Hi All, I am writing a shell script for which I am stuck with an extraction part. I arrived till extraction of a path of file. Lets take an example. I now have a file which contains following one line: 2348/home/userid/mydir/any_num_dir/myfile.text Now I want to extract only... (2 Replies)
Discussion started by: start_shell
2 Replies

8. Shell Programming and Scripting

Extracting a string from one file and searching the same string in other files

Hi, Need to extract a string from one file and search the same in other files. Ex: I have file1 of hundred lines with no delimiters not even space. I have 3 more files. I should get 1 to 10 characters say substring from each line of file1 and search that string in rest of the files and get... (1 Reply)
Discussion started by: mohancrr
1 Replies

9. Shell Programming and Scripting

extracting from a string

How do I extract 5th to 10th characters of string as given below stored in a shell variable. "ab cd ef gh ij kl" How is cut to be used on this? Thanks for any help. (1 Reply)
Discussion started by: preetikate
1 Replies

10. Shell Programming and Scripting

Extracting String from a list

I have a string of Data such as Date Time Tran1 Tran2 Tran3 ...... How can I extract just all the trans excluding Date and Time. Thanks, Odogbolu98 ;) (4 Replies)
Discussion started by: odogbolu98
4 Replies
Login or Register to Ask a Question