Text Search and grouping in perl