This will change only the first appearance of "dev" to "stage"
sed -i '0,/dev/ s/dev/stage/' config.inc.php
Be aware that it changes "devel" into "stageel". This version behaves just like you want, only a "dev" is searched, not a "devel" (in fact, s/\<dev\>/stage/ as the substitution expression should work, but it does not seem to work as expected? I'll be glad if anyone with more sed-fu can explain. )
sed -i "/\<dev\>/,/\<dev\>/ s/dev/stage/" config.inc.php
For logging:
date >> /path/to/dev/run.log
Added by Jonathan Leffler
- Assuming other issues are resolved (see below), the second
sed command can still change devel to stagel if the line contains, for example, "move devel code from /some/dev/location to /some/stage/location".
- Also, the second
sed command will map each dev found between the first line containing dev and the second such line. This matters if there's more than one matching line, whereas the original '0,/dev/' (or amended '0,/\<dev\>/') only matches the first line as requested.
- The reason
"s/\<dev\>/stage/" doesn't work is not a sed issue but a shell issue. Use single quotes and you'd be almost OK. With double quotes, the back-slash less-than sequence appears to sed as just less-than.
- Rule of Thumb: use single quotes around any argument in a shell script containing regular expression material. Unlesss it is saturated with single quotes, replace each single quote in the regular expression with the sequence quote, backslash, quote, quote "
'\''". (The first quote terminates the single quote string; the backslash quote is a single quote; the last quote restarts the single quote string.)
- Note that the '
-i' option is a GNU extension to sed; it is a legimate part of the answer since the question is tagged Linux, where GNU sed is used; be aware if you need to move to a platform such as Solaris, AIX, HP-UX.
- Finally,
sed does not support extended regular expressions as standard; you have to explicitly enable them in GNU sed with the '-r' option.
In my estimation, assuming overwrite is desirable, the command should be:
sed -i -r '0,/\<dev\>/s/\<dev\>/stage/' config.inc.php