# 2 Loops gathering input

**URL:** <https://community.unix.com/t/2-loops-gathering-input/365970>\
**Category:** Shell Programming and Scripting\
**Created:** [April 5, 2017, 11:44am UTC](https://community.unix.com/t/2-loops-gathering-input/365970 "2017-04-05T11:44:26Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeffs42885](https://community.unix.com/letter_avatar/jeffs42885/32/6_449c533f586939b6e5f246632a46421f.png) [@jeffs42885](https://community.unix.com/u/jeffs42885)\
**Post date:** [April 5, 2017, 11:44am UTC](https://community.unix.com/t/2-loops-gathering-input/365970/1 "2017-04-05T11:44:26Z")

</div>

Greetings all,

I have came up with some code, to read an input file and generate a list, here is a sample of what I am working with. The variable $USER will be inputted by the person running the utility.

```nohighlight
 do
 folder=${line%}
 echo "$folder $USER
 done < list.txt

```

sample of list.txt-

```nohighlight
folder1
folder2
folder3

```

User inputs-

```nohighlight
Tom

```

sample of output of running the loop-

```nohighlight
folder1 Tom
folder2 Tom
folder3 Tom

```

What I want to accomplish, is insted of manually entering a user name, Tom in the example, I want to be able to input a list. Here is the sample output:

List of users-

```nohighlight
Tom
Harry
Sally
Joe

```

Desired Output

```nohighlight
folder1 Tom
folder2 Tom
folder3 Tom
folder1 Harry
folder2 Harry
folder3 Harry
folder1 Sally
folder2 Sally
folder3 Sally
folder1 Joe
folder2 Joe
folder3 Joe
                 

```

What is the best way to approach this. I am thinking two loops but I am still not sure. Help is appreciated. Thanks!

---

<div class="post-metadata">

**Author:** ![vbe](https://community.unix.com/user_avatar/community.unix.com/vbe/32/266_2.png) [@vbe](https://community.unix.com/u/vbe)\
**Post date:** [April 5, 2017, 12:27pm UTC](https://community.unix.com/t/2-loops-gathering-input/365970/2 "2017-04-05T12:27:54Z")

</div>

2 loops is the way to go ( a loop within a loop...)

---

<div class="post-metadata">

**Author:** ![jeffs42885](https://community.unix.com/letter_avatar/jeffs42885/32/6_449c533f586939b6e5f246632a46421f.png) [@jeffs42885](https://community.unix.com/u/jeffs42885)\
**Post date:** [April 5, 2017, 1:01pm UTC](https://community.unix.com/t/2-loops-gathering-input/365970/3 "2017-04-05T13:01:42Z")

</div>

I am not exactly sure how to approach this. what if my list folders has a list of 4, and my list of users has 3.. thats where I got stuck doing the loop within a loop.

---

<div class="post-metadata">

**Author:** ![vbe](https://community.unix.com/user_avatar/community.unix.com/vbe/32/266_2.png) [@vbe](https://community.unix.com/u/vbe)\
**Post date:** [April 5, 2017, 1:45pm UTC](https://community.unix.com/t/2-loops-gathering-input/365970/4 "2017-04-05T13:45:29Z")

</div>

Maybe if you shown us what you did so far, and the output, we could help you in understanding what isnt working for you and show you the way to go...

---

<div class="post-metadata">

**Author:** ![jeffs42885](https://community.unix.com/letter_avatar/jeffs42885/32/6_449c533f586939b6e5f246632a46421f.png) [@jeffs42885](https://community.unix.com/u/jeffs42885)\
**Post date:** [April 5, 2017, 1:50pm UTC](https://community.unix.com/t/2-loops-gathering-input/365970/5 "2017-04-05T13:50:18Z")

</div>

Here is what I am thinking..

```nohighlight
do
 folder=${line%}
      user=${line%}
      echo "$folder $user.txt
 done <user.txt
 done < list.txt

```

---

<div class="post-metadata">

**Author:** ![migurus](https://community.unix.com/letter_avatar/migurus/32/6_449c533f586939b6e5f246632a46421f.png) [@migurus](https://community.unix.com/u/migurus)\
**Post date:** [April 5, 2017, 2:05pm UTC](https://community.unix.com/t/2-loops-gathering-input/365970/6 "2017-04-05T14:05:22Z")

</div>

that should do it :

```nohighlight
for a in $(cat /tmp/folders); do
        for b in $(cat /tmp/users); do
                echo $a" "$b;
        done;
done

```

---

<div class="post-metadata">

**Author:** ![jeffs42885](https://community.unix.com/letter_avatar/jeffs42885/32/6_449c533f586939b6e5f246632a46421f.png) [@jeffs42885](https://community.unix.com/u/jeffs42885)\
**Post date:** [April 5, 2017, 2:18pm UTC](https://community.unix.com/t/2-loops-gathering-input/365970/7 "2017-04-05T14:18:51Z")

</div>

Another thing that I came up with-

```plaintext
while read -u 3 -r groups.txt && read -u 4 -r folders.txt; do
		group={$line%}
		folder={$line%}
		  echo "$folder $user"
done 3<list1..txt 4<list2.txt

```

How do I make sure that group and folder within the loop point to the right text file?

---------- Post updated at 01:18 PM ---------- Previous update was at 01:06 PM ----------

> [@migurus](#):
>
> that should do it :
> 
> ```plaintext
> for a in $(cat /tmp/folders); do
> for b in $(cat /tmp/users); do
> echo $a" "$b;
> done;
> done
> 
> ```

is giving me-

```plaintext

[jeff@server:/home/jeff/test]> ./for.ksh >> for.out
[jeff@server:/home/jeff/test]> cat for.out
P.P..Pfolders.txtProups.txt P.P..Pfolders.txtProups.txt
[jeff@server:/home/jeff/test]>

```

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [April 5, 2017, 2:44pm UTC](https://community.unix.com/t/2-loops-gathering-input/365970/8 "2017-04-05T14:44:28Z")

</div>

Implied double loop - `bash 4` needed:

```nohighlight
eval printf "%s\\\n" {$(sed -n '1h; 1!H; ${g;s/\n/,/g; p; }' file1)}\" \"{$(sed -n '1h; 1!H; ${g;s/\n/,/g; p; }' file2)}
folder1 Tom
folder1 Harry
folder1 Sally
folder1 Joe
folder2 Tom
folder2 Harry
folder2 Sally
folder2 Joe
folder3 Tom
folder3 Harry
folder3 Sally
folder3 Joe

```

Beware of the evil `eval` for the usual, well known reasons...

---

<div class="post-metadata">

**Author:** ![jeffs42885](https://community.unix.com/letter_avatar/jeffs42885/32/6_449c533f586939b6e5f246632a46421f.png) [@jeffs42885](https://community.unix.com/u/jeffs42885)\
**Post date:** [April 5, 2017, 3:43pm UTC](https://community.unix.com/t/2-loops-gathering-input/365970/9 "2017-04-05T15:43:54Z")

</div>

> [@rudic](#):
>
> Implied double loop - `bash 4` needed:
> 
> ```plaintext
> eval printf "%s\\\n" {$(sed -n '1h; 1!H; ${g;s/\n/,/g; p; }' file1)}\" \"{$(sed -n '1h; 1!H; ${g;s/\n/,/g; p; }' file2)}
> folder1 Tom
> folder1 Harry
> folder1 Sally
> folder1 Joe
> folder2 Tom
> folder2 Harry
> folder2 Sally
> folder2 Joe
> folder3 Tom
> folder3 Harry
> folder3 Sally
> folder3 Joe
> 
> ```
> 
> Beware of the evil `eval` for the usual, well known reasons...

Still getting the same error. Here is the output I am getting with that:

```plaintext
{P.P..Pfolders.txtProups.txt} {P.P..Pfolders.txtProups.txt}

```

Here is my script currently, the list of folders is being added to an XML file, and i need to add groups too. take a look (Bold is what I need to add):

```plaintext
		   while read line
			   do
			   folder=${line%}
		       echo "<folder name = \"$folder\" group name = \"$group\">" >> output.xml
			   echo "</folder>" >> output.xml
		    done < folders.txt

```

This is AIX by the way

---------- Post updated at 02:43 PM ---------- Previous update was at 01:54 PM ----------

I think I may be overthinking this. I divided this into two seperate loops:

```plaintext
while read line
do
group=${line%}
echo "<group=\"$group\">" >> Output.xml
done < groups.txt

while read line
do
group=${line%}
echo "<group=\"$group\">" >> Output.xml
done < groups.txt

```

My output looks as expected:

```plaintext
<folder name = "Folder3">
</folder>
<folder name = "Folder2">
</folder>
<folder name = "Folder1">
</folder>
<folder name = "Folder0">
</folder>
<file group="TestGroup3">
</file>
<file group="TestGroup2">
</file>
<file group="TestGroup1">
</file>

```

Now, I need to get each of those 3 groups after the start and end tag of folder..

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [April 5, 2017, 3:51pm UTC](https://community.unix.com/t/2-loops-gathering-input/365970/10 "2017-04-05T15:51:47Z")

</div>

wouldn't it be easier in awk:

```nohighlight
awk 'FNR==NR {f[$0];next} {for (i in f) print i,$0}' folderFile userFile

```

---

<div class="post-metadata">

**Author:** ![jeffs42885](https://community.unix.com/letter_avatar/jeffs42885/32/6_449c533f586939b6e5f246632a46421f.png) [@jeffs42885](https://community.unix.com/u/jeffs42885)\
**Post date:** [April 5, 2017, 3:59pm UTC](https://community.unix.com/t/2-loops-gathering-input/365970/11 "2017-04-05T15:59:14Z")

</div>

> [@vgersh99](#):
>
> wouldn't it be eassier in awk:
> 
> ```plaintext
> awk 'FNR==NR {f[$0];next} {for (i in f) print i,$0}' folderFile userFile
> 
> ```

Yeah, I have played with a few things in awk and sed but I cannot get a viable solution.

where in that snippet that you provided would I place where I want to place the inserted line?

Currently have:

```plaintext
<folder name = "Folder3">
</folder>

```

Need:

```plaintext
<folder name = "Folder3">
    <file group="TestGroup1">
    </file>
</folder>

```

Thanks!

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [April 5, 2017, 4:08pm UTC](https://community.unix.com/t/2-loops-gathering-input/365970/12 "2017-04-05T16:08:19Z")

</div>

how about:

```nohighlight
awk -v qq='"' '
   FNR==NR {u[$0];next}
  {
      printf("<folder name = %s%s%s>\n",qq,$0,qq)
      for (i in u) 
         printf("\t<file group =%s%s%s></file>\n",qq,i,qq)
      print "</folder>"
  }' userFile.txt folderFile.txt

```

---

<div class="post-metadata">

**Author:** ![jeffs42885](https://community.unix.com/letter_avatar/jeffs42885/32/6_449c533f586939b6e5f246632a46421f.png) [@jeffs42885](https://community.unix.com/u/jeffs42885)\
**Post date:** [April 5, 2017, 4:13pm UTC](https://community.unix.com/t/2-loops-gathering-input/365970/13 "2017-04-05T16:13:01Z")

</div>

> [@vgersh99](#):
>
> how about:
> 
> ```plaintext
> awk -v qq='"' '
> FNR==NR {u[$0];next}
> {
> printf("<folder name = %s%s%s>\n",qq,$0,qq); for (i in u) printf("\t<file group =%s%s%s></file>\n",qq,i,qq); print "</folder>"
> }' userFile.txt folderFile.txt
> 
> ```

Looks like that works, help is much appreciated to everyone in this post that helped me!! :b:

---

<div class="post-metadata">

**Author:** ![durden\_tyler](https://community.unix.com/user_avatar/community.unix.com/durden_tyler/32/1450_2.png) [@durden\_tyler](https://community.unix.com/u/durden_tyler)\
**Post date:** [April 5, 2017, 4:41pm UTC](https://community.unix.com/t/2-loops-gathering-input/365970/14 "2017-04-05T16:41:20Z")

</div>

```nohighlight
$
$ cat folders.txt
Folder3
Folder2
Folder1
$
$ cat groups.txt
TestGroup3
TestGroup2
TestGroup1
$
$ perl -lne '$s ? do{push @g,$_} : do{push @f,$_; $s=1 if eof} }{
             for $i (@f){
                 print "<folder name = \"$i\">";
                 for $j (@g){ print " <file group = \"$j\"></file>"}
                 print "</folder>"
             }' folders.txt groups.txt
<folder name = "Folder3">
    <file group = "TestGroup3"></file>
    <file group = "TestGroup2"></file>
    <file group = "TestGroup1"></file>
</folder>
<folder name = "Folder2">
    <file group = "TestGroup3"></file>
    <file group = "TestGroup2"></file>
    <file group = "TestGroup1"></file>
</folder>
<folder name = "Folder1">
    <file group = "TestGroup3"></file>
    <file group = "TestGroup2"></file>
    <file group = "TestGroup1"></file>
</folder>
$
$
 

```

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [April 6, 2017, 2:58am UTC](https://community.unix.com/t/2-loops-gathering-input/365970/15 "2017-04-06T02:58:59Z")

</div>

Slower but simpler perhaps, with regular shell scripting:

```nohighlight
while read folder
do
  printf '<folder name = "%s">\n' "$folder"
  while read group
  do  
    printf ' <file group = "%s"></file>\n' "$group"
  done < groups.txt
  printf '</folder>\n'
done < folders.txt > Output.xml

```

---

<div class="post-metadata">

**Author:** ![migurus](https://community.unix.com/letter_avatar/migurus/32/6_449c533f586939b6e5f246632a46421f.png) [@migurus](https://community.unix.com/u/migurus)\
**Post date:** [April 6, 2017, 5:25pm UTC](https://community.unix.com/t/2-loops-gathering-input/365970/16 "2017-04-06T17:25:30Z")

</div>

suspect some typo on your part.  
under regular ksh88 and bash 3 it works:

```nohighlight
/tmp$ cat users
Tom
Harry
Sally
Joe
/tmp$ cat folders
folder1
folder2
folder3
/tmp$ cat x.ksh
for a in $(cat /tmp/folders); do
        for b in $(cat /tmp/users); do
                echo $a" "$b;
        done;
done
/tmp$ ./x.ksh
folder1 Tom
folder1 Harry
folder1 Sally
folder1 Joe
folder2 Tom
folder2 Harry
folder2 Sally
folder2 Joe
folder3 Tom
folder3 Harry
folder3 Sally
folder3 Joe

```
