Shell script to read a file and store in variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script to read a file and store in variables
# 1  
Old 09-02-2013
Shell script to read a file and store in variables

I have a input file like this.

Sample.txt

Code:
30    | TXDatacenter | TXBackupDC
10    | UKDatacenter | UKBackupDC
0      | NLDatacenter | NLBackupDC
......
......
......


I need to get these values in different variables like this.
Code:
Load1=30
PriCenter1=TXDatacenter
BkpCenter1=TXBackupDC

Load2=10
PriCenter2=UKDatacenter
BklCenter2=UKBackupDC
....
...

Basically, I am looking to separate the contents of the file based on words. But i want to store the values in to variables based on its line number. (i.e Load1, 2,3 & its corresponding PriCenter1,2,3)

Any help is appreciated.

Thanks,
Visha

Last edited by Don Cragun; 09-02-2013 at 04:18 PM.. Reason: Add CODE tags
# 2  
Old 09-02-2013
Try something like (bash):
Code:
i=1
while read Load[i] x PriCenter[i] x BkpCenter[i]
do 
  (( i++ ))
done < file

Code:
$ echo "${PriCenter[2]}"
UKDatacenter

The variable $x is used as a dummy variable..

Last edited by Scrutinizer; 09-02-2013 at 04:41 PM..
# 3  
Old 09-02-2013
The following does what you want (assuming that your request to store the 3rd value in the 2nd line in BklCenter2 was a typo and you meant BkpCenter2) and shows that it worked. But I generally find that setting variables like this in a shell script isn't really what you want to do. It is usually much better to process a line at a time. If that can't be done for some reason, arrays are usually much easier to handle than variable names that have to be constructed on the fly (unless your script knows that there will always be the same number of input lines and that you want to process the values found on each line in a different way).
Code:
eval $(awk -F' *[|] *' '
{       printf("Load%d=\"%s\"\nPriCenter%d=\"%s\"\nBkpCenter%d=\"%s\"\n",
                NR, $1, NR, $2, NR, $3)
}' Sample.txt)
set|grep '^Load'
set|grep '^PriCenter'
set|grep '^BkpCenter'

If you want to try this on a Solaris/SunOS system, use /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk instead of awk.
# 4  
Old 09-03-2013
Thanks. I prefer to use arrays. Can you guide me on that.

Thanks,
Visha
# 5  
Old 09-03-2013
Using awk arrays:

Code:
awk -F"|" '{Load[NR]=$1;PriCenter[NR]=$2;BkpCenter[NR]=$3} END{for(i in Load) {print Load[i], PriCenter[i], BkpCenter[i]}}' inputfile

# 6  
Old 09-03-2013
Quote:
Originally Posted by Visha
Thanks. I prefer to use arrays. Can you guide me on that.

Thanks,
Visha
Scrutinizer has already shown you how to use shell array variables (as long as you're using a shell that supports array variables (e.g., bash and ksh).

And, krishmaths has already shown you how to set up awk arrays (although the sample code given for this will include spaces in the variables that don't seem to be wanted).

Without knowing what you intend to do with this data (other than store it in an array), we have no way to know whether either of these suggestions will do what you need. So, what are you really trying to do?

Last edited by Don Cragun; 09-03-2013 at 01:30 PM.. Reason: Add note about limitation on krishmaths's awk array solution.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read variables from a text file for use in csh script

Hello, I have a text file (say, declarevars.txt) that contains multiple lines that are essentially meant to be variable declarations: set arr1 = (var1a var1b var1c) set arr2 = (var2a var2b var2c) . . . I want to be able to read this text file within a csh (sorry) script and have that... (2 Replies)
Discussion started by: arjaydj
2 Replies

2. Shell Programming and Scripting

Help Needed: UNIX shell variables to store Oracle table records

Hello Folks, I'm working on a requirement to automate the process of generating report(csv file) using metadata info stored in an Oracle table and E-mail it to respective people. Meta data table: Report_ID,Report_SUB_ID,Report_DB,Report_SQL,Report_to_email_Id 1,1,DEV,'select * From... (2 Replies)
Discussion started by: venkat_reddy
2 Replies

3. Linux

How to store count of multiple queries in variables in a shell script?

how to store the count of queries in variables inside a filein shell script my output : filename ------- variable1=result from 1st query variable2=result from 2nd query . . . . (3 Replies)
Discussion started by: sanvel
3 Replies

4. Shell Programming and Scripting

[Doubt] How can I store numbers less than 1? Shell variables

Hi everyone, I am having some problems with my scripts so I hope you could help me. I am trying to store the result of a division in a variable in tcshell but I have the problem that if: For example, dividing 2/100 the result is 0.02 but if I store that I have "0". How can I have 0.02... (8 Replies)
Discussion started by: Bloody
8 Replies

5. Shell Programming and Scripting

Store text file into a datastructure using Shell script

i have a text file and want to store it in a appropriate data structure (2-d is preferable) . The contents are as follows.. plzzz suggest an appropriate way to store the contents by using shell scripting (bash shell) text file Abc Def Ghi Hjk Lmn Opq Rst Uvw .... ..... .... .... ....... (3 Replies)
Discussion started by: cynosure2009
3 Replies

6. Shell Programming and Scripting

How to store contents of a command in array of variables in shell script?

I want to store contents of command dir in array of variables For eg: dir contents are command d2 demovi~ file inven java new untitled folder d1 demovi er1 filename inven~ myfiles ubuntu desktop xmms ----------------------------------- I... (3 Replies)
Discussion started by: netresearch
3 Replies

7. UNIX for Dummies Questions & Answers

trouble using read to store values in variables from command output

I know there are caveats about using read in pipelines because read is treated by a subshell. I know this but I can't think of any way to accomplish this regardless, I'm still a rookie. I hope somebody will be able to interpret what it is that I'm trying to accomplish and correct me. ... (2 Replies)
Discussion started by: ProGrammar
2 Replies

8. Shell Programming and Scripting

how can i read text file and assign its values to variables using shell

Hello, I have a cat.dat file, i would like shell to read each 3 lines and set this 3 lines to 3 different variables. my cat.dat is: 11 12 +380486461001 12 13 +380486461002 13 14 +380486461003 i want shell to make a loop and assign 1st line to student_id, 2nd line to... (4 Replies)
Discussion started by: rosalinda
4 Replies

9. Shell Programming and Scripting

To read and separate number and words in file and store to two new file using shell

hi, I am a begginer in unix and i want to know how to open a file and read it and separate the numbers & words and storing it in separate files, Using shell scripting. Please help me out for this. Regards S.Kamakshi (2 Replies)
Discussion started by: kamakshi s
2 Replies

10. Shell Programming and Scripting

Awk/shell question: Read from file and assign to variables.

Is anyone able to help with writing a program that will do the following: 1. Read the contents of a file, line by line, and on each line, assign each of the two columns to a shell variable. 2. perform an action on the variables 3. Read the next line. Here is what I've gotten so far. ... (3 Replies)
Discussion started by: akbar
3 Replies
Login or Register to Ask a Question