#!/usr/bin/perl use strict; use warnings; my $file = '/path/to/file'; my $content = do { local (@ARGV,$/) = $file; <> }; my $keyword = 'test'; my $replace = 'hallo'; $content =~ s/$keyword \s+ [^\s]+/$keyword $replace/xg; print $content;