I have written a very simple bash script to help me migrate from dev to staging. What it does is it deletes all files in staging, copies the files over from dev to stage.
However, the config.inc.php file needs to have the first instance of "dev" to be changed to "stage", and no other instance changed.
Second, everytime I run it (I run the script from the dev directory), i'd like it to write a log back in the dev directory which will append the date/time stamp that I ran the staging bash script into this log.
Thanks.
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
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 ". 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. "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. '\\''
". (The first quote terminates the single quote string; the backslash quote is a single quote; the last quote restarts the single quote string.) -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. 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
To change a word in a file:
cat config.inc.php | sed 's:dev::stage' > config.inc.php.new
To append to a log file:
echo $timestamp >> mylogfile.log
The first part (editing your file) can be done well with the stream editor utility sed
:
sed -i -e s/dev/stage/ config.inc.php
This edits the file in-place ( -i
) with no backup, using expression /dev/stage/
( -e
) to replace one expression with another.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.