Reading from a file and assigning values


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Reading from a file and assigning values
# 1  
Old 08-14-2008
Reading from a file and assigning values

HI
I have something like this in a file
ABC = 1
DEF = 2
GHI = 3
JKL = 4
MNO = 5
QRS = 6
TUV = 7

I need to assign ABC to V_abc (that is to a variable)
GHI to V_ghi (that is to another variable)
TUV to say V_tuv
What i mean to say is that i have many values in a file and i need some of them

I tried grep but it is not working
Any help is appreciated
Thanks in advance
# 2  
Old 08-14-2008
Code:
eval `sed 's/ //g;s/^/v_/' filename`

The sed script removes the spaces and prepends v_ to each line in the file, and then eval evaluates it in the current shell, which causes the variables' values to be assigned.
# 3  
Old 08-15-2008
Hi
even if i give the way you have given
eval `sed 's/ //g;s/^/v_/' filename`
it is appending v_ to everything in the file
but not printing the v_abc value on the console

if i give in the following way
eval `sed -e 's/abc/v_abc/g' ${dirold}/eng/file23.txt`
then also it is not printing the value
# 4  
Old 08-17-2008
Of course it is not printing the value. You said nothing in your original post about printing values, all you asked for was to assign them to a variable.

If you echo $v_ABC after running the command I gave you, it should display "1".
# 5  
Old 08-17-2008
awk '{print "V_"tolower($1),$2}' filename > newfile
your new file will have
V_abc=1
V_def=2 so on....
use or redirect it to your acript inside script they work as variables :-)
# 6  
Old 08-17-2008
Don't you mean:

Code:
awk '{print "V_"tolower($1)"="$2}' filename > newfile

# 7  
Old 08-17-2008
oh ya exactly.. thanks for correcting me.. annihilannic.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Reading Xml file and print the values into the text file in columnwise?

hi guys, i want help... Reding XML file and print the values into the text file using linux shell script file as per below xml file <sequence> <Filename>aldorzum.doc</Filename> <DivisionCode>US</DivisionCode> <ContentType>Template</ContentType> <ProductCode>VIMZIM</ProductCode> </sequence>... (4 Replies)
Discussion started by: sravanreddy
4 Replies

2. UNIX for Dummies Questions & Answers

Reading XML file and print the values in the text file using Linux shell script

hi guys, i want help... Reding XML file and print the values into the text file using linux shell script file as per below xml file <sequence> <Filename>aldorzum.doc</Filename> <DivisionCode>US</DivisionCode> <ContentType>Template</ContentType> <ProductCode>VIMZIM</ProductCode> </sequence>... (1 Reply)
Discussion started by: sravanreddy
1 Replies

3. Shell Programming and Scripting

Reading and Comparing values of file

Hi gurus.. Am reading a file, counting number of lines and storing it in a variable. Then am passing that variable into If loop for comparision, if the number of lines are greater than 1000 it should split a file if not it should send the file name to archive folder.. but when i execute the... (4 Replies)
Discussion started by: azherkn3
4 Replies

4. Shell Programming and Scripting

Reading off values from a large file

Hi, I have a large output file (star.log), with many lines of the following type *** T vavg unburnt: 723.187 / burnt: 2662.000 What I would like to do is pick the values 723.187 and 2662.000 and What I've got so far is awk '/unburnt:.*burnt:/{Tu=$6;Tb=$NF}END{print Tu, Tb}'... (6 Replies)
Discussion started by: lost.identity
6 Replies

5. Shell Programming and Scripting

Reading from a file and assigning to an array in perl

I wrote a simply perl that searched a file for a particualr value and if it found it, rite it and the next three lines to a file. Now I have been asked to check those next three lines for a different value and only write those lines if it finds the second value. I was thinking the best way to... (1 Reply)
Discussion started by: billprice13
1 Replies

6. Shell Programming and Scripting

Reading values from a file

Hi I have a file in the following format AFUE 0. AOXI 0. VFUE 100.0 VOXI 274.601 TFUE 298. TOXI 2229.544 TMAX 2400. What I want to do is write a bash script, that use either perl/awk or sed to read the number after VFUE and VOXI (which is 100.0 and... (1 Reply)
Discussion started by: lost.identity
1 Replies

7. Shell Programming and Scripting

Bash: Reading a file and assigning variables from file

I have a file that has four values on each line and I'd like to give each column a variable name and then use those values in each step of a loop. In bash, I believe you could use a while loop to do this or possibly a cat command, but I am super new to programming and I'm having trouble decoding... (2 Replies)
Discussion started by: ccorder22
2 Replies

8. Shell Programming and Scripting

Help in assigning values to variables from the file

Hi! This might be a simple thing, but I'm struggling to assign values to variables from the file. I've the following values stored in the file.. It consists of only two rows.. 10 20 I want to assign the first row value to variable "n1" and the second row value to variable "n2".. That is ... (3 Replies)
Discussion started by: abk07
3 Replies

9. Shell Programming and Scripting

Reading data from file and assigning to variable

I was trying to store the number of lines in a file and store it in a file.after that i want to store the information in a file to a variable which is further used in the if loop to check certain condition. #!/bin/bash cat <file> | wc -l > count.txt x="$count.txt"; i=10; if ; then cat... (10 Replies)
Discussion started by: sudhakaryadav
10 Replies

10. Shell Programming and Scripting

Reading file and assigning that to Variable

I am missing something here, I have a file which contains only one line and that is either a number or character string. I am trying to read the file and assign that value to a variable and here it seems I am missing something and not getting the expected results... Here is the code : #!/bin/ksh... (2 Replies)
Discussion started by: Vaddadi
2 Replies
Login or Register to Ask a Question