#!/usr/local/bin/perl -w use strict; my $text="Twenty centuries of stony sleep were vexed to nightmare"; my $numwords=5; my $replacement="lead to a crick in the back."; print $text; $text=~ s/^((\w+\s){$numwords}).*$/$1.$replacement/e; print $text; __END__ Twenty centuries of stony sleep were vexed to nightmare Twenty centuries of stony sleep lead to a crick in the back.