retrive value from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting retrive value from a file
# 1  
Old 05-17-2008
Java retrive value from a file

hi
i am new to shell scripting

i have a properties file like
hs=abc
hs1=def
hs2=ghi

now i want to retrive each value and assign it to a variable
like var1 = abc

please help
# 2  
Old 05-17-2008
If the file's syntax is compatible with the shell's, you can simply use the source command, the lone dot:

Code:
. file.cf

With the example you have provided, this would assign "abc" to the variable hs, "def" to hs1, and "ghi" to hs2.

If that's not what you want, you could perhaps read in one line at a time, remove everything up through the first equals sign, and then assign the result to some variable.

Code:
for var in var1 var2 var3
do
    read line
    eval $var=\""${line#*=}"\"
done <file.cf

This reads three lines and assigns anything after the first equals sign on each of those lines to the selected variable name; successively, var1, var2, and var3.

The use of eval is a rather advanced technique; I would advise against this if you can come up with a simpler solution.
# 3  
Old 05-17-2008
You mean this ?

Code:
for val in $(cut -f 2 -d'=' buf); do var1=$val; echo $var1; done

# 4  
Old 05-17-2008
Java retrieve a value fron a file

hi era
i am able to retrieve a value from a file, but i dont know how to assign the retrieved value to a variable. the code i have is
#regressiontest.cfg
hs=abc
hs1=def
hs2=ghi
now i am retrieving the value from the above file
RetrieveCfgvalue()
{
CFG_VALUE=`grep "$2=" $CONFIG_FILE_NAME | cut -d"=" -f2`
CFG_VALUE=`echo $CFG_VALUE|sed -s "s/ *//g"`
CFG_VALUE=`echo $CFG_VALUE|sed -s "s/\"//g"`
if [ ! -z $CFG_VALUE ]
then
eval "$1=$CFG_VALUE"
fi
}

CONFIG_FILE_NAME="regressiontest.cfg"
RetrieveCfgvalue hs hs

now i want to assign the retrieved value(hs) to a variable like
var1 = abc
please help quickly
# 5  
Old 05-17-2008
Your function looks like it already does this. After RetrieveCfgvalue hs hs you should have the value of hs in the variable hs, isn't that correct? Then if you also want to assign it to var1, you can say var1=$hs (or simply RetrieveCfgvalue var1 hs directly, if that was what you were planning anyway).
# 6  
Old 05-17-2008
Java retrieve a alue from a file

hi era
the proble occuring is i have
hs=lpdma520.dev.ipc.us.aexp.com
hs1=lpdma521.dev.ipc.us.aexp.com
hs2=lpdma522.dev.ipc.us.aexp.com
like this in the cfg file.
When i tried to retrieve the value by using the above function
i am getting "command not found"
can u please help
# 7  
Old 05-17-2008
Based on the additional information in the duplicate thread https://www.unix.com/shell-programmin...#post302196206 I would say the problem is that you can't have full stops in variable names.

Please don't post duplicate threads.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script (sh file) logic to compare contents of one file with another file and output to file

Shell script logic Hi I have 2 input files like with file 1 content as (file1) "BRGTEST-242" a.txt "BRGTEST-240" a.txt "BRGTEST-219" e.txt File 2 contents as fle(2) "BRGTEST-244" a.txt "BRGTEST-244" b.txt "BRGTEST-231" c.txt "BRGTEST-231" d.txt "BRGTEST-221" e.txt I want to get... (22 Replies)
Discussion started by: pottic
22 Replies

2. Shell Programming and Scripting

Compare 2 text file with 1 column in each file and write mismatch data to 3rd file

Hi, I need to compare 2 text files with around 60000 rows and 1 column. I need to compare these and write the mismatch data to 3rd file. File1 - file2 = file3 wc -l file1.txt 58112 wc -l file2.txt 55260 head -5 file1.txt 101214200123 101214700300 101250030067 101214100500... (10 Replies)
Discussion started by: Divya Nochiyil
10 Replies

3. UNIX for Dummies Questions & Answers

How to retrive data from DB(Aqua studio) in CVS format using UNIX script?

I am using aqua studio DB. I need to retrive the data from my database using uxin script in .csv format. i am using select query along with the joins. my o/p in the DB is of the below format. Cycle IDCycle StatusRecord 98N-0000ACV23-3636FCliet Level (Af)Success1689393HF-J7879-09090RCliet Level... (1 Reply)
Discussion started by: Mugivz
1 Replies

4. Shell Programming and Scripting

Match list of strings in File A and compare with File B, C and write to a output file in CSV format

Hi Friends, I'm a great fan of this forum... it has helped me tone my skills in shell scripting. I have a challenge here, which I'm sure you guys would help me in achieving... File A has a list of job ids and I need to compare this with the File B (*.log) and File C (extend *.log) and copy... (6 Replies)
Discussion started by: asnandhakumar
6 Replies

5. UNIX for Dummies Questions & Answers

make ls retrive ordered list of elements

Hello friends!! I have a question regarding the use of ls in unix. I have a folder with files: t1.txt t2.txt t3.txt t4.txt ... t10.txt When I make an ls I always get: t10.txt t1.txt t2.txt t3.txt .. t9.txt (2 Replies)
Discussion started by: SaktiPhoenix
2 Replies

6. Shell Programming and Scripting

Retrive the value returned by a command excuted in a remote server using ssh

Hello Everybody, I'm facing a weird problem with the awk command. I try to retrieve in a variable the value returned by a simple ls command. ls /export/home/tmp |tail -1 return a good value (the name of the . But When I try to execute the same command in a remote server using ssh as... (2 Replies)
Discussion started by: Jabarod
2 Replies

7. Shell Programming and Scripting

retrive lines from a file using AWK command

Dear friends, please tell me how to get some required lines from a file and write to another file using AWK command. i.e., if a file contains, abcdefghigk 12345 lmnopqrstuv 678910 wxyz please tell me how to get lines(line count is always 2 and it's contineous) mentioned in blue... (1 Reply)
Discussion started by: swamymns
1 Replies

8. Shell Programming and Scripting

to retrive data that appear only once in a file.

hi, I need to get the list of functions that are used more than once in a file list. Thanks in advance (1 Reply)
Discussion started by: anibu
1 Replies

9. UNIX for Advanced & Expert Users

Retrive deleted file's info

I have shell script which reads files, stores its data into Oracle and then deletes tht file. now i want to know creation date and time of deleted files. can we do this? if yes then how? (4 Replies)
Discussion started by: asmita
4 Replies

10. Shell Programming and Scripting

help to retrive data from log file

hi i have following file. where i m trying to retrive data on latest date. let us say we are extracting data from this file for Jun 30 where date is highest date in log file. here i want to take output in other file from first line of Jun 30 to the end of file in short i want retrive... (5 Replies)
Discussion started by: d_swapneel14
5 Replies
Login or Register to Ask a Question