# loop issue

**URL:** <https://community.unix.com/t/loop-issue/203311>\
**Category:** Shell Programming and Scripting\
**Created:** [July 2, 2008, 7:40am UTC](https://community.unix.com/t/loop-issue/203311 "2008-07-02T07:40:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kkkk](https://community.unix.com/letter_avatar/kkkk/32/6_22734026db12803ebb3e059622ac3534.png) [@kkkk](https://community.unix.com/u/kkkk)\
**Post date:** [July 2, 2008, 7:40am UTC](https://community.unix.com/t/loop-issue/203311/1 "2008-07-02T07:40:49Z")

</div>

I have 2 files one of them has all the all mac addresses and the other one has all the ip addresses. Basically, I want to loop thru those 2 files and generate a configuration like below:

host [www184.domain.com](http://www184.domain.com) {  
hardware ethernet 00:13:72:3B:B4:3A;  
fixed-address 192.168.0.184;  
}

any feedbacks?

Thanks

---

<div class="post-metadata">

**Author:** ![Sergiu-IT](https://community.unix.com/user_avatar/community.unix.com/sergiu-it/32/176_2.png) [@Sergiu-IT](https://community.unix.com/u/Sergiu-IT)\
**Post date:** [July 2, 2008, 8:05am UTC](https://community.unix.com/t/loop-issue/203311/2 "2008-07-02T08:05:57Z")

</div>

Hi !

Asuming that you are sure that you have the same number of lines on both files, and every record is on a single line, you can use the following script:

```nohighlight
#!/usr/bin/perl -w
#

open(MACS,"mac.txt") or die("Could not open mac.txt");
open(IPS,"ip.txt") or die("Could not open ip.txt");
open(RESULT,">result.txt") or die("Could not open result.txt");

my @macs = ();
my @ips = ();

push(@macs,$_) while( <MACS> );
push(@ips,$_) while( <IPS> );

chomp(@macs);
chomp(@ips);

for( $i=0 ; $i<=$#macs ; $i++ ){
    print RESULT "host www$i.domain.com {\n";
    print RESULT "hardware ethernet $macs[$i];\n";
    print RESULT "fixed-address $ips[$i];\n";
    print RESULT "}\n\n";
}

close(MACS);
close(IPS);
close(RESULT);

```

---

<div class="post-metadata">

**Author:** ![fpmurphy](https://community.unix.com/user_avatar/community.unix.com/fpmurphy/32/110_2.png) [@fpmurphy](https://community.unix.com/u/fpmurphy)\
**Post date:** [July 2, 2008, 1:27pm UTC](https://community.unix.com/t/loop-issue/203311/3 "2008-07-02T13:27:35Z")

</div>

Is the hostname common to both files?

---

<div class="post-metadata">

**Author:** ![kkkk](https://community.unix.com/letter_avatar/kkkk/32/6_22734026db12803ebb3e059622ac3534.png) [@kkkk](https://community.unix.com/u/kkkk)\
**Post date:** [July 3, 2008, 2:52am UTC](https://community.unix.com/t/loop-issue/203311/4 "2008-07-03T02:52:47Z")

</div>

> [@sergiu-it](#):
>
> Hi !
> 
> Asuming that you are sure that you have the same number of lines on both files, and every record is on a single line, you can use the following script:
> 
> ```plaintext
> #!/usr/bin/perl -w
> #
> 
> open(MACS,"mac.txt") or die("Could not open mac.txt");
> open(IPS,"ip.txt") or die("Could not open ip.txt");
> open(RESULT,">result.txt") or die("Could not open result.txt");
> 
> my @macs = ();
> my @ips = ();
> 
> push(@macs,$_) while( <MACS> );
> push(@ips,$_) while( <IPS> );
> 
> chomp(@macs);
> chomp(@ips);
> 
> for( $i=0 ; $i<=$#macs ; $i++ ){
> print RESULT "host www$i.domain.com {\n";
> print RESULT "hardware ethernet $macs[$i];\n";
> print RESULT "fixed-address $ips[$i];\n";
> print RESULT "}\n\n";
> }
> 
> close(MACS);
> close(IPS);
> close(RESULT);
> 
> ```

Thanks for the script. It works great...

---

<div class="post-metadata">

**Author:** ![kkkk](https://community.unix.com/letter_avatar/kkkk/32/6_22734026db12803ebb3e059622ac3534.png) [@kkkk](https://community.unix.com/u/kkkk)\
**Post date:** [July 3, 2008, 3:00am UTC](https://community.unix.com/t/loop-issue/203311/5 "2008-07-03T03:00:41Z")

</div>

> [@fpmurphy](#):
>
> Is the hostname common to both files?

The hostname is actually the last octet of the ip. I ended up creating a separate hostname file and was able to get the final result with Sergiu-IT's script.
