$ perl -nE ' chomp; my @x = split /\|/; my ($y) = grep $x[$_] =~ /,/, 0..$#x; for (split /,/, $x[$y]) { splice @x, $y, 1, $_; say join "|", @x; } ' a,b,c|d|e a|d|e b|d|e c|d|e a|b,c,d|e a|b|e a|c|e a|d|e a|b|c,d,e a|b|c a|b|d a|b|e #### my @z = split /,/, $x[$y]; for (@z) { #### for (split /,/, $x[$y]) {