#!perl use strict; my @from =(); my @to =(); my $re = qr/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/; while (){ next unless /(?:to|from):$re/; push @from,($_ =~ /from:($re)/g); push @to, ($_ =~ /to:($re)/g); } for (@from){ print "$_ ,".(shift @to)."\n" } __DATA__ hshhhgljlkjglgkjj from:198.18.66.5 aegighighighilg to:198.18.66.6 iiufduifuiuih from:198.18.66.7 hiihhj to:198.18.66.8 hhouhoho from:198.18.66.9 igilgojhjh to:198.18.66.10 igighliho from:198.18.66.11 highighioouhouhhhoh to:198.18.66.12