The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
foreach loop + 2 variables JamesGoh Shell Programming and Scripting 3 06-04-2008 03:36 AM
using variables outside a while loop Tornado Shell Programming and Scripting 2 02-09-2007 01:26 AM
adding variables for, for loop jazz High Level Programming 3 03-13-2006 04:23 AM
Writing to variables within a loop Sniper Pixie UNIX for Dummies Questions & Answers 0 03-02-2006 09:11 AM
for loop with multiple variables ? fosterian Shell Programming and Scripting 3 03-01-2004 05:15 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-30-2008
marcpascual marcpascual is offline
Registered User
  
 

Join Date: Sep 2005
Posts: 33
SH: two variables in for loop

Hi, say I have a simple sh script like this:

Code:
for i in a b c d
        do
                for j in 1 2 3 4
                        do
                                echo "$i $j"
                        done
        done
and the output is

Code:
 a 1
 a 2
 a 3
 a 4
 b 1
 b 2
 b 3
 b 4
 c 1
 c 2
 c 3
 c 4
 d 1
 d 2
 d 3
 d 4
how do i make its output like this?

Code:
a 1
b 2
c 3
d 4
TIA
  #2 (permalink)  
Old 07-30-2008
cfajohnson's Avatar
cfajohnson cfajohnson is online now Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,313

Don't use two loops.

Code:
n=1
for l in a b c d
do
   printf "%s %d\n" "$l" "$n"
   n=$(( $n + 1 ))
done
  #3 (permalink)  
Old 07-30-2008
marcpascual marcpascual is offline
Registered User
  
 

Join Date: Sep 2005
Posts: 33
Thanks, I need to re-qualify my question:

what if its not a number and you can't do math on it?

e.g.
list1:
mary
eve
delilah

list2:
joseph
adam
samson

output:
mary joseph
adam eve
samson delilah


for i in `cat list1`; ... for j in `cat list2`; ... do ... print $i $j ... done

any way around this?
  #4 (permalink)  
Old 07-30-2008
cfajohnson's Avatar
cfajohnson cfajohnson is online now Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,313
Quote:
Originally Posted by marcpascual View Post
Thanks, I need to re-qualify my question:

what if its not a number and you can't do math on it?

e.g.
list1:
mary
eve
delilah

list2:
joseph
adam
samson

output:
mary joseph
adam eve
samson delilah


for i in `cat list1`; ... for j in `cat list2`; ... do ... print $i $j ... done

any way around this?
Code:
set -f
IFS='
'
set -- $( cat list2 )
for i in `cat list1`
do
  printf "%s %s\n" "$i" "$1"
  shift
done
  #5 (permalink)  
Old 07-30-2008
marcpascual marcpascual is offline
Registered User
  
 

Join Date: Sep 2005
Posts: 33
amazing, thank you!
  #6 (permalink)  
Old 07-30-2008
danmero danmero is offline Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,369
Quote:
Originally Posted by cfajohnson View Post
Code:
set -f
IFS='
'
set -- $( cat list2 )
for i in `cat list1`
do
  printf "%s %s\n" "$i" "$1"
  shift
done
KISS principle states that design simplicity should be a key goal and unnecessary complexity avoided.
Code:
paste -d ' ' list1 list2

Last edited by danmero; 07-30-2008 at 11:40 PM..
  #7 (permalink)  
Old 08-01-2008
Exolon Exolon is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 2
Quote:
Originally Posted by marcpascual View Post
how do i make its output like this?

Code:
a 1
b 2
c 3
d 4
It seems you can't have multiple "x in y" terms in a for loop, so the next best thing is probably to rewrite the loop as an index iterator:
Code:
is=(a b c d)
js=(1 2 3 4)

for ((i = 0; i < 4; i++)) 
	do echo ${is[i]} ${js[i]}
done
It's not as nice, but still relatively easy to read, especially if you define your lists together.
Sponsored Links
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 06:53 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0