#!/usr/bin/perl use warnings; use strict; my @pi = split //, '1415926535'; while () { 1 while s/ ([^ ]*?)\Kx/$pi[length $1]/; print; } __DATA__ /bin/grep .......... /path/to/some/file /bin/grep xxxxxxxxxx /path/to/some/file /bin/grep x....x...x /path/to/some/file