Sorting a list

I am trying to sort a list

If you walk through the list, every you have passed both website1 and website2 and get back to website1, the last lines should be collected into one line and the process should start again.

The following:

http://www.website1.com
http://www.website1.com
http://www.website2.com
http://www.website2.com
http://www.website1.com
http://www.website2.com
http://www.website1.com
http://www.website1.com
http://www.website2.com

should become:

http://www.website1.com;http://www.website1.com;http://www.website2.com;http://www.website2.com
http://www.website1.com;http://www.website2.com
http://www.website1.com;http://www.website1.com;http://www.website2.com

I wouldn't call this sorting. But the following brute force awk script seems to do what your want:

awk '
NR == 1 {
        l1 = $0
        printf("%s", $0)
        combine = 1
        next
}
l1 == $0 {
        printf("%s%s", combine ? ";" : "\n", $0)
        combine = 1
        next
}
{       printf(";%s", $0)
        combine = 0
}
END {   print ""
}' file

As always, if you're using a Solaris/SunOS system, use /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk instead of awk .

1 Like
my @arr;
my $hit2=0;
while(<DATA>){
 chomp;
 if(/website1/ && scalar @arr && $hit2){
	print join(";",@arr);
	print "\n";
	$#arr=-1;
	$hit2=0;
 }
 elsif(/website2/){
	$hit2=1;
 }
 push @arr,$_;
}
print join(";",@arr) if scalar @arr;
__DATA__
http://www.website1.com
http://www.website1.com
http://www.website2.com
http://www.website2.com
http://www.website1.com
http://www.website2.com
http://www.website1.com
http://www.website1.com
http://www.website2.com