LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Help with looping more than 1 variable. (https://www.linuxquestions.org/questions/linux-newbie-8/help-with-looping-more-than-1-variable-698941/)

hdoyle 01-21-2009 02:10 PM

Help with looping more than 1 variable.
 
Hi,

I'm stuck trying to write (what I thought would be simple) a script that reads 2 files into 2 variables then loops. The problem I have is that the loop only affects the 1st variable/file until it's finished and then continues so I end up with a lot of output that's useless.

The idea is to read the files (one of which contains id's and the other ip addresses) and then have the loop transfer from id to ip one line at a time.

Here is my current script.
#!/bin/sh
DIR=/home/nice/logs
for i in `cat $DIR/siteip.lst`;do
for j in `cat $DIR/siteid.lst`;do
echo "$i - $j"
done
done

I would like the echo output like this.
81.149.235.94 - 1
79.173.138.243 - 6
81.149.223.174 - 8
82.108.163.50 - 9
87.244.75.158 - 10
217.46.206.228 - 11
194.73.124.94 - 13
82.110.147.110 - 33
193.95.167.190 - 34
81.150.196.3 - 37

instead i get this
81.149.235.94 - 1
81.149.235.94 - 6
81.149.235.94 - 8
81.149.235.94 - 9
81.149.235.94 - 10
81.149.235.94 - 11
81.149.235.94 - 13
81.149.235.94 - 33
81.149.235.94 - 34
81.149.235.94 - 37

as you can see the 1st ip stays the same while the j variable counts.

Any help would be great.

TIA

Didier Spaier 01-21-2009 04:14 PM

Iterate using 'cut -f<field_number> -d<field delimiter>' if all id / ip are on the same line.

If there is one line for each id / ip, use 'read'.

vladmihaisima 01-21-2009 04:27 PM

If you have two files with the same number of lines you can use the 'paste' command. This will merge lines from first and second file. If you want them to be separated by '-' you can use.

Code:

paste -d- a.txt b.txt
Use 'man paste' for more information.

If you do not want to use the paste command, you could obtain something similar to your script using the following script:

Code:

#!/bin/bash

exec 3<> a.txt
exec 4<> b.txt

R1=0
R2=0
while [ $R1 -eq 0 -a $R2 -eq 0 ]; do
read A <&3
R1=$?
read B <&4
R2=$?
echo $A - $B
done;

Some explanations:
- the execs opens the two files and asigns them to file descriptors 3 and 4
- in R1 and R2 we store the results status of the read from the files
- the read command read in variables A (and B) a line from files descriptors 3 and 4

Note about your code: you have the for loops one into the other. So the 'inner' loop will execute completly for 1 iteration of the outer loop. This is not what you want....

Didier Spaier 01-21-2009 04:58 PM

Or you can use arrays:
Code:

#!/bin/sh
declare -a idp
idp=(`cat siteip.lst`)
element_count=${#idp[@]}
declare -a ids
ids=(`cat siteid.lst`)
index=0
while [ "$index" -lt "$element_count" ]
do
  echo -n ${idp[$index]}
  echo ${ids[$index]}
  let "index=$index+1"
done

I suppose you have the same number of lines in both files, otherwise edit the script accordingly.

Idea borrowed to Mendel Cooper. Go get the good stuff at The Linux Documentation Project guides

hdoyle 01-22-2009 05:28 AM

Thanks
 
Nice one chaps. I now have it working. Thank you very much.


All times are GMT -5. The time now is 02:05 PM.