my $string ="test\nI want length of this line\n test"; my $position = 12; # Note this postion can be any position in any line. currently considering it inside 2nd line my @newlines; # for storing \n positions push @newlines, 0; while ($string =~/\n/g) { push @newlines, pos($string); } my itr = scalar @newlines - 1; while ($newlines[$itr] > $position) { $itr--; } my $length_of_line = $newlines[$itr + 1] - $newlines[$itr]; #Better efficient solution to find length of line if we have only any position inside it. Thanks in advance!!!