像这样的东西也可能起作用:
#!/bin/bash
s=http://localhost.com
r=http://example.com
cd /path/to/MyApp
grep -rl "$s" * | while read f; do
sed -i "s|$s|$r|g" "$f"
done
编辑:或者不是,因为你刚刚从bash切换到了batch-file。批处理解决方案可能如下所示:
@echo off
setlocal EnableDelayedExpansion
for /r "C:\path\to\MyApp" %%f in (*.chtml) do (
(for /f "tokens=*" %%l in (%%f) do (
set "line=%%l"
echo !line:
)) >"%%~ff.new"
del /q "%%~ff"
ren "%%~ff.new" "%%~nxf"
)
但是,批量执行此操作确实非常难看(也容易出错),并且最好使用sed
for Windows,或者(更好)在 PowerShell 中执行此操作:
$s = "http://localhost.com"
$r = "http://example.com"
Get-ChildItem "C:\path\to\MyApp" -Recurse -Filter *.chtml | ForEach-Object {
(Get-Content $_.FullName) |
ForEach-Object { $_ -replace [regex]::Escape($s), $r } |
Set-Content $_.FullName
}
请注意,-Filter
仅适用于 PowerShell v3。对于早期版本,您必须这样做:
Get-ChildItem "C:\path\to\MyApp" -Recurse | Where-Object {
-not $_.PSIsContainer -and $_.Extension -eq ".chtml"
} | ForEach-Object {
(Get-Content $_.FullName) |
ForEach-Object { $_ -replace [regex]::Escape($s), $r } |
Set-Content $_.FullName
}