#!/usr/bin/perl -w use strict; my (@desc, @data, @new); while(<>) { chomp; if(($_ eq "Description:") .. ($_ eq "Data:")) { push @desc, $_; next unless $_ eq "Data:"; } # if you have another tag if(($_ eq "Data:") .. ($_ eq "NewTag:")) { push @data, $_; next unless $_ eq "NewTag:"; } # and so on until the last tag push @new, $_; } shift @desc; # get rid of "Description:" from front. pop @desc if @data; # get rid of "Data:" as last element. shift @data; # get rid of "Data:" from front. pop @data if @new; # get rid of "NewTag:" as last element. shift @new; # get rid of "NewTag:" from front. print "desc: @desc\n"; print "data: @data\n"; print "new: @new\n";