reading more than one variable into a for loop


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers reading more than one variable into a for loop
# 1  
Old 10-24-2008
reading more than one variable into a for loop

Hi,

I have a file (details.txt) with 3 rows of variables ie...

name postcode age

john D fr25dd 25
mark W ab122aa 22
phil C cd343bb 33

What I want to do is read down the list with a loop and add each field into a one line piece of text...

So I have a file (test1) which reads;



my name is john D. I am 25 years old..... postcode is fr25dd
my name is mark W. I am 22 years old..... postcode is ab122aa
my name is phil C. I am 33 years old..... postcode is cd343bb



I don't know how to read more than one variable at a time, so I tried splitting the file, but it dies when trying to read the second variable $j ... What's the best way to do this?

Works with just the $i variable.

What I tried....
------------------------------------------------------------------------------------------
awk < details.txt '{print $1}' > name.test
awk < details.txt '{print $2}' > postcode.test
awk < details.txt '{print $3}' > age.test


for i in `cat name.test`
for j in `cat postcode.test`
for k in `cat age.text`

do

echo "my name is $i. I am $3 years old..... postcode is $2" > test1

done

rm name.test
rm postcode.test
rm age.test
------------------------------------------------------------------------------------------
# 2  
Old 10-24-2008
Hammer & Screwdriver solution with awk

Code:
> cat file11
john D fr25dd 25
mark W ab122aa 22
phil C cd343bb 33
> awk '{print "My name is "$1" "$2". I am "$4" years old.... postcode is "$3}' <file11
My name is john D. I am 25 years old.... postcode is fr25dd
My name is mark W. I am 22 years old.... postcode is ab122aa
My name is phil C. I am 33 years old.... postcode is cd343bb

# 3  
Old 10-24-2008
With bash:

Code:
while read -ra r; do
  printf "my name is %s, I'm %d years old, postcode is %s\n" \
  "${r[0]} ${r[1]}" "${r[3]}" "${r[2]}"
done<infile

Otherwise:

Code:
while read -r l f c a; do
  printf "my name is %s, I'm %d years old, postcode is %s\n" \
  "$l $f" "$a" "$c"
done<infile


Last edited by radoulov; 10-24-2008 at 12:38 PM..
# 4  
Old 10-24-2008
many thanks for the quick replies

Thanks for those answers - will probably use awk, but will take a look how the others work as well. Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Bash Variable scope - while loop while reading from a file

Cope sample1: test.sh i=0 echo " Outside loop i = $i " while do i=$(( $i + 1)) echo "Inside loop i = $i " done echo " Out of loop i is : $i " When run output : Outside loop i = 0 Inside loop i = 1 Inside loop i = 2 Inside loop i = 3 Inside loop i = 4 Inside loop i = 5 Inside... (8 Replies)
Discussion started by: Adarshreddy01
8 Replies

2. Shell Programming and Scripting

Reading line in while loop

Hello Team, i have to read line by line in a while loop, and the input file has:. # The script will start cppunit test application to run unit tests. -LD_LIBRARY_PATH=$CPPUNIT_HOME/lib:\ +LD_LIBRARY_PATH=$VOBTAG/SS_UnitTest/lib:\ $VOBTAG/SS_BFD/BFDSCLI/build:\ ... (7 Replies)
Discussion started by: chandana hs
7 Replies

3. Shell Programming and Scripting

[Solved] How to increment and add variable length numbers to a variable in a loop?

Hi All, I have a file which has hundred of records with fixed number of fields. In each record there is set of 8 characters which represent the duration of that activity. I want to sum up the duration present in all the records for a report. The problem is the duration changes per record so I... (5 Replies)
Discussion started by: danish0909
5 Replies

4. Shell Programming and Scripting

Array Variable being Assigned Values in Loop, But Gone when Loop Completes???

Hello All, Maybe I'm Missing something here but I have NOOO idea what the heck is going on with this....? I have a Variable that contains a PATTERN of what I'm considering "Illegal Characters". So what I'm doing is looping through a string containing some of these "Illegal Characters". Now... (5 Replies)
Discussion started by: mrm5102
5 Replies

5. Shell Programming and Scripting

for loop - reverse reading

All, Here is my for loop export CFGLIST="LIST1 LIST2 LIST3" for i in $CFGLIST do echo print $i done The output will be LIST1 LIST2 LIST3 But i want it display LIST3 LIST2 LIST1 (8 Replies)
Discussion started by: baluchen
8 Replies

6. Shell Programming and Scripting

How to get the modified value of variable outside the while loop reading from a file

Hi Friends , Sorry if this is a repeated question , The input file contains 5 lines , so the the values of the variables i and count should b i=5; count=15 but the variables are not updating , the value of variables showing i=0 and count =0 only.:mad: can any1 help me please. (11 Replies)
Discussion started by: babusek
11 Replies

7. Shell Programming and Scripting

Reading variable from file variable values

Hi, Here is the output of lpstat. I would like to read value of Queue which is(abxxxxb1)and status that is DOWN in first line. i dont care what is in second line. any one can help me.thanks Queue Dev Status Job Files User PP % Blks Cp Rnk ------- ----- ---------... (5 Replies)
Discussion started by: sagii
5 Replies

8. Shell Programming and Scripting

Not access variable outside loop when a reading a file

I am writing a shell script using the korn shell. It seems that I am only able to use local variables within a while loop that is reading a file. (I can't access a variable outside a previously used while loop.) It's been a while since I wrote shell scripts. Here is a sample cat file.txt... (4 Replies)
Discussion started by: ricardo.ludwig
4 Replies

9. Shell Programming and Scripting

while loop not reading last line

hi all, i have a while loop which i am using to read lines into an array: k=0 exec 10<file while read LINE <&10; do ARRAY=$LINE ((k++)) done exec 10>&- echo ${ARRAY} for some reason when i display the array it is not showing the last row in the file. any help appreciated. (1 Reply)
Discussion started by: npatwardhan
1 Replies

10. Shell Programming and Scripting

reading from 2 files through while loop

hi i have two files cat input.txt 123456| 43256 456482|5893242 cat data.txt xv 123456 abcd dsk sd 123456 afsfn dd df 43256 asdf ff ss 456482 aa sf 5893242 ff ff aa 5893242 aa aa i need to read inputs from input.txt and find data for data.txt. then i need to print them as a... (2 Replies)
Discussion started by: windows
2 Replies
Login or Register to Ask a Question