Picking contents value of file at run time based on variable values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Picking contents value of file at run time based on variable values
# 1  
Old 03-12-2012
Picking contents value of file at run time based on variable values

HI,

I have to write a unix script and need your help.

in my application where I have to invoke this script a varialble is there where the value comes in a variable . for example variable can be var=ABC
like this there will be any type of value in the vcariable.
there is a unix directory structure where
/home/source/files/
this directory has files like
abc.txt and content of file are
source_directory=/home/abc/source/
target_directory=/home/abc/target/
temp_directory=/home/abc/temp/
arch_directory=/home/abc/archive/

I have to fecth the value of source_directory, target_directory,temp_directory, arch_directory at run time and use this value for further processing.

if Value of variable will be xxx and the file name will be xxx.txt from where values will be picked and values available in file will be

xxx.txt and content of file are
source_directory=/home/xxx/source/
target_directory=/home/xxx/target/
temp_directory=/home/xxx/temp/
arch_directory=/home/xxx/archive/
# 2  
Old 03-12-2012
Code:
 
$ cat temp.cf
source_directory=/home/xxx/source/
target_directory=/home/xxx/target/
temp_directory=/home/xxx/temp/
arch_directory=/home/xxx/archive/
$ cat test.sh
#!/bin/bash
echo -n "Enter the variable : "
read var
echo "Variable value is : $var"
sed "s/xxx/$var/" temp.cf > $var.txt
. $var.txt
echo $source_directory
echo $target_directory
echo $temp_directory
echo $arch_directory
$ ./test.sh 
Enter the variable : ABC
Variable value is : ABC
/home/ABC/source/
/home/ABC/target/
/home/ABC/temp/
/home/ABC/archive/

# 3  
Old 03-12-2012
Thanks Kamraj, i will try this and let you know if i will face any issue

---------- Post updated at 05:21 AM ---------- Previous update was at 05:06 AM ----------

Hi Kamraj,
I have some scenario where value is different ... like for any file like india.txt values are
Code:
source_directory=/home/india/temp/source/
target_directory=/home/india/temp/target/
temp_directory=/home/india/temp/
arch_directory=/home/xxx/archive/

for some type of files this is changing ... means directory where file is present is one and number of values in the file is same but its values is dfferent..
for source directory, in this case how to pick the values from the file at run time?


Moderator's Comments:
Mod Comment Please use next time code tags for your code and data

Last edited by vbe; 03-12-2012 at 09:59 AM.. Reason: code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Picking the latest file based on a timestamp for a Dynamic file name

Hi , I did the initial search but could not find what I was expecting for. 15606Always_9999999997_20160418.xml 15606Always_9999999998_20160418.xml 15606Always_9999999999_20160418.xml 9819Always_99999999900_20160418.xml 9819Always_99999999911_20160418.xmlAbove is the list of files I... (4 Replies)
Discussion started by: chillblue
4 Replies

2. Shell Programming and Scripting

Folder contents getting appended as strings while redirecting file contents to a variable

Hi one of the output of the command is as below # sed -n "/CCM-ResourceHealthCheck:/,/---------/{/CCM-ResourceHealthCheck:/d;/---------/d;p;}" Automation.OutputZ$zoneCounter | sed 's/$/<br>/' Resource List : <br> *************************** 1. row ***************************<br> ... (2 Replies)
Discussion started by: vivek d r
2 Replies

3. Programming

Python:Picking values from a table

Hi I trying to write a python script that executes a command to screen scrub results below I will appreciate it very much if you can help me with a python script that can pick the percentage USAGE in the second column based on the supplied queue number in the first column Thanks in advance. ... (2 Replies)
Discussion started by: kaf3773
2 Replies

4. Shell Programming and Scripting

Transpose timestamp based on column values and calculate time difference

Hello Expert, I need to transpose Date-Timestamp based on same column values and calculate time difference. The input file would be as below and required output is mentioned in the bottom INPUT File ======== 08/23/2012 12:36:09 JOB_5340 08/23/2012 12:36:14 JOB_5340 08/23/2012... (2 Replies)
Discussion started by: asnandhakumar
2 Replies

5. Shell Programming and Scripting

Accepting multiple values in a variable at run time

Hi, Below is starting entry of my script #!/bin/ksh Usage() { print "Usage: $0 ID OPTION SERVER" print "<br>Where :" print "<br>Enter your ID into PARAM1, OPTION in the PARAM2 and SERVER in the PARAM3 field" print "<br>ID should be a valid ID" print "<br>OPTION should be either... (2 Replies)
Discussion started by: gopajitmalakar
2 Replies

6. Shell Programming and Scripting

Picking values from a file.

Hi, I have a file which contains values in this format. abc cde fgh ijk lmn opq rst uvw The user will pass the required parameter from the command line. My requirement is that script should pick the values passed by the user and the next value in the next line. Like if the user... (10 Replies)
Discussion started by: arijitsaha
10 Replies

7. UNIX for Dummies Questions & Answers

Sorting files based on timestamp and picking the latest file

Hi Friends, Newbie to shell scripting Currently i have used the below to sort data based on filenames and datestamp $ printf '%s\n' *.dat* | sort -t. -k3,4 filename_1.dat.20120430.Z filename_2.dat.20120430.Z filename_3.dat.20120430.Z filename_1.dat.20120501.Z filename_2.dat.20120501.Z... (12 Replies)
Discussion started by: robertbrown624
12 Replies

8. Shell Programming and Scripting

Urgent ...pls Sorting files based on timestamp and picking the latest file

Hi Friends, Newbie to shell scripting. Currently i have used the below to sort data based on filenames and datestamp $ printf '%s\n' *.dat* | sort -t. -k3,4 filename_1.dat.20120430.Z filename_2.dat.20120430.Z filename_3.dat.20120430.Z filename_1.dat.20120501.Z filename_2.dat.20120501.Z... (1 Reply)
Discussion started by: robertbrown624
1 Replies

9. UNIX for Dummies Questions & Answers

count values based on contents of another file

Hello, I have two files as shown below: test1 678 679 689 690 710 test2 1 678 654 800 676 791 689 900 I want to get a count of lines from test2 whose columns bound the values in test1 I tried running the code below; however am getting wrong results. (3 Replies)
Discussion started by: Gussifinknottle
3 Replies

10. Shell Programming and Scripting

Picking the file based on Date..Requirement

Dear frnds My requirement is as follows -rw-r----- 1 f02 dd 109428250 May 18 14:02 Extracts_20070518104730.zip -rw-r----- 1 f02 dd 109493187 May 21 13:30 Extracts_20070521091700.zip -rw-r----- 1 f02 dd 109993058 May 23 14:14 Extracts_20070523085955.zip -rw-r----- 1... (5 Replies)
Discussion started by: sureshg_sampat
5 Replies
Login or Register to Ask a Question