Insert line after match using sed
Note the standard sed
syntax (as in POSIX, so supported by all conforming sed
implementations around (GNU, OS/X, BSD, Solaris...)):
sed '/CLIENTSCRIPT=/a\CLIENTSCRIPT2="hello"' file
Or on one line:
sed -e '/CLIENTSCRIPT=/a\' -e 'CLIENTSCRIPT2="hello"' file
(-e
xpressions (and the contents of -f
iles) are joined with newlines to make up the sed script sed
interprets).
The -i
option for in-place editing is also a GNU extension, some other implementations (like FreeBSD's) support -i ''
for that.
Alternatively, for portability, you can use perl
instead:
perl -pi -e '$_ .= qq(CLIENTSCRIPT2="hello"\n) if /CLIENTSCRIPT=/' file
Or you could use ed
or ex
:
printf '%s\n' /CLIENTSCRIPT=/a 'CLIENTSCRIPT2="hello"' . w q | ex -s file