http://qs321.pair.com?node_id=675331


in reply to add after $1

You are reading in single lines and then testing if that line has an 'if' and a 'fi' in it. Since the if statement you are looking for spans multiple lines you won't find it that way.

Your choices are to read in the entire script to a single string or to process the file a line at a time remembering the state of your search. The first is easiest and, assuming your script is not several hundred Megabytes in size, practical.

use strict; use warnings; my $here=<<HERE; if /usr/bin/pgrep -x -u 0 -P 1 ltps& >/dev/null 2>&1; +then echo "$0: ltps is already running" exit 0 fi HERE my $file = do { local $/; <DATA> }; $file =~ s/^(if.*?rctladm\.conf.*?fi$)/$1\n$here/ms; print $file; __DATA__ # Run rctladm to configure system resource controls based on the setti +ngs # previously saved by rctladm. See rctladm(1m) for instructions on ho +w to # modify resource control settings. # if [ -f /etc/rctladm.conf ] && [ -x /usr/sbin/rctladm ]; then /usr/sbin/rctladm -u fi
produces
# Run rctladm to configure system resource controls based on the setti +ngs # previously saved by rctladm. See rctladm(1m) for instructions on ho +w to # modify resource control settings. # if [ -f /etc/rctladm.conf ] && [ -x /usr/sbin/rctladm ]; then /usr/sbin/rctladm -u fi if /usr/bin/pgrep -x -u 0 -P 1 ltps& >/dev/null 2>&1; +then echo "Perl-1.pl: ltps is already running" exit 0 fi
You may want to look at the indenting of your replacement text.