LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-21-2009, 03:10 PM   #1
hdoyle
LQ Newbie
 
Registered: Jan 2009
Location: London
Distribution: fc10
Posts: 3

Rep: Reputation: 0
Post 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
 
Old 01-21-2009, 05:14 PM   #2
Didier Spaier
LQ Addict
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slint64-14.2 on Lenovo Thinkpad W520
Posts: 7,867

Rep: Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780
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'.

Last edited by Didier Spaier; 01-21-2009 at 05:25 PM.
 
Old 01-21-2009, 05:27 PM   #3
vladmihaisima
Member
 
Registered: Oct 2002
Location: Delft, Netherlands
Distribution: Gentoo
Posts: 196

Rep: Reputation: 33
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....
 
Old 01-21-2009, 05:58 PM   #4
Didier Spaier
LQ Addict
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slint64-14.2 on Lenovo Thinkpad W520
Posts: 7,867

Rep: Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780
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

Last edited by Didier Spaier; 01-22-2009 at 06:51 AM. Reason: Typo corrected and reference added
 
Old 01-22-2009, 06:28 AM   #5
hdoyle
LQ Newbie
 
Registered: Jan 2009
Location: London
Distribution: fc10
Posts: 3

Original Poster
Rep: Reputation: 0
Smile Thanks

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Sed search for variable and delete entire line, but variable contains forward /'s Passions Programming 2 11-10-2008 04:44 PM
Looping in a program. googleee Programming 3 11-05-2008 11:23 PM
login looping plaircpa Slackware 3 10-31-2005 09:10 PM
looping in mplayer zexter Linux - Software 1 12-22-2003 05:39 PM
[C] for looping problem wuck Programming 6 10-19-2003 05:29 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:50 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration