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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk/shell question: Read from file and assign to variables.
# 1  
Old 05-06-2008
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. it doesn't work, and I only present it to give a better idea of what I am getting at:

for i in `cat inputfile|eval \`awk '{print "HOST="$1; print "PASS="$2;}'\``
do
echo "Action: host is $HOST and pass is $PASS"
echo
done



Any assistance is greatly appreciated
akbar
# 2  
Old 05-06-2008
I think I've got a solution:

while read line; do
echo $line | eval `awk '{print "export HOST="$1; print "export PASS="$2;}'`
echo "Host is $HOST and PASS is $PASS"

done < $PASSLIST
# 3  
Old 05-06-2008
how about
Code:
while read HOST PASS
do
      echo "HOST = $HOST  PASS = $PASS"
done < $PASSLIST

# 4  
Old 05-07-2008
Quote:
Originally Posted by jim mcnamara
how about
Code:
while read HOST PASS
do
      echo "HOST = $HOST  PASS = $PASS"
done < $PASSLIST

Excellent! Thank you.

akbar
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to read each line from input file, assign variables, and echo to output file?

I've got a file that looks like this (spaces before first entries intentional): 12345650-000005000GL140227 ANNUAL HELC FEE EN 22345650-000005000GL140227 ANNUAL HELC FEE EN 32345650-000005000GL140227 ANNUAL HELC FEE EN I want to read through the file line by line,... (6 Replies)
Discussion started by: Scottie1954
6 Replies

2. Shell Programming and Scripting

Read record from the text file contain multiple separated values & assign those values to variables

I have a file containing multiple values, some of them are pipe separated which are to be read as separate values and some of them are single value all are these need to store in variables. I need to read this file which is an input to my script Config.txt file name, first path, second... (7 Replies)
Discussion started by: ketanraut
7 Replies

3. Shell Programming and Scripting

How to read a two files, line by line in UNIX script and how to assign shell variable to awk ..?

Input are file and file1 file contains store.bal product.bal category.bal admin.bal file1 contains flip.store.bal ::FFFF:BADC:CD28,::FFFF:558E:11C5,6,8,2,1,::FFFF:81C8:CA8B,::FFFF:BADC:CD28,1,0,0,0,::FFFF:81C8:11C5,2,1,0,0,::FFFF:81DC:3111,1,0,1,0 store.bal.... (2 Replies)
Discussion started by: veeruasu
2 Replies

4. Shell Programming and Scripting

Shell script to read a file and store in variables

I have a input file like this. Sample.txt 30 | TXDatacenter | TXBackupDC 10 | UKDatacenter | UKBackupDC 0 | NLDatacenter | NLBackupDC ...... ...... ...... I need to get these values in different variables like this. Load1=30 PriCenter1=TXDatacenter... (5 Replies)
Discussion started by: Visha
5 Replies

5. Shell Programming and Scripting

Unable to read assign values to two variables in while loop

I am trying to read a input file which has two columns separated by space Input file server1 server2 server3 server4 server5 server6 When i execute the below while code it reads line by line and a and b variables are able to successfully fetch the values while read a b do echo "$a" echo... (5 Replies)
Discussion started by: chidori
5 Replies

6. Shell Programming and Scripting

Read record from the text file & assign those values to variables in the script

For eg: I have sample.txt file with 4 rows of record like: user1|password1 user2|password2 user3|password3 user4|password4 The username and password is sepsrated by '|' I want to get the 1st row value from the file and assign it to two different variables(username and password) in my... (1 Reply)
Discussion started by: priya001
1 Replies

7. Shell Programming and Scripting

Read variables names from array and assign the values

Hi, I have requirement to assign values to variables which are created dynamically. Below is the code which i am using to achieve above requirement. #!/bin/ksh oIFS="$IFS"; IFS=',' STR_FAIL_PARENT_IF_FAILS="WF_F_P_IF_FAILS1,WF_F_P_IF_FAILS2,WF_F_P_IF_FAILS3" set -A... (1 Reply)
Discussion started by: tmalik79
1 Replies

8. Shell Programming and Scripting

How to read a delimited string and assign fields to incremented variables?

How can I read a string delimited on spaces and assign the fields to incremented variables. For example: Given $exts= txt dat mov I want to read in $exts and have "txt" "dat" and "mov" assigned to incremented variables like $ext1, $ext2, etc. I would like to do this in a loop so that I can... (4 Replies)
Discussion started by: runit
4 Replies

9. Shell Programming and Scripting

How to read a file and assign variables to data?

Hi, I need a simple csh script to read a file containing data like this Buy Transactions : 175 Sell Transactions : 212 Server: sepo2 i want to read both field and its value and assign variables to each.. (2 Replies)
Discussion started by: pravsripad
2 Replies

10. 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
Login or Register to Ask a Question