2013-06-13 26 views
2

替換特定的字母我有一個創建一個autosys工作觸發一個批處理文件,該文件會發現一個特定的字符串,然後替換下一個4個字符。查找字符串,並以批處理文件

例如,如果文件(多線)以下具有內容和我正在尋找扮演

瘋狂的狐狸跳老藤和 踢足球。

我應該以「INFA」

我是新來的批處理文件和我的帶動下一直堅持到我這樣做只是使用批處理文件替換「SOCC」。任何幫助將大大apprciated。

感謝, 喜悅

+0

[批處理文件中的字符串替換]的可能的重複(http://stackoverflow.com/questions/2772456/string-replacement-in-batch-file) – jeb

+0

它不一定是SOCC。發生起到之後的任何4個字母應該INFA –

+0

Windows命令行來替代不具備搜索和替換工具。下載'sed'或編寫一個shell腳本。 – Endoro

回答

0

讓自己的sed副本your OS從一個批處理文件調用它。

sed -e 's/socc/INFA/g' inputFileName > outputFileName 
+0

thanx的建議,但按照該clind政策我不允許使用任何外部公用事業.... –

+0

另一種方法是使用PowerShell或VBScript,您可以從調用你的批處理文件。 – Joe

1

我用這個時常:sar.bat

::Search and replace 
@echo off 
if "%~3"=="" (
echo.Search and replace 
echo Syntax: 
echo "%~nx0" "filein.txt" "fileout.txt" "regex" "replace_text" [first] 
echo. 
echo.EG: change the first time apple appears on each line, to orange 
echo."%~nx0" "my text old.txt" "my text changed.txt" "apple" "orange" first 
echo. 
echo.People that are familiar with regular expressions can use some: 
echo. 
echo.Change every line starting from old (and everything after it^) to new 
echo."%~nx0" "my text old.txt" "my text changed.txt" "old.*" "new" 
echo. 
echo.If [first] is present only the first occurrence per line is changed 
echo. 
echo.To make the search case sensitive change 
echo.IgnoreCase= from True to False 
echo. 
pause 
goto :EOF 
) 
if "%~5"=="" (set global=true) else (set global=false) 
set s=regex.replace(wscript.stdin.readall,"%~4") 
>_.vbs echo set regex=new regexp 
>>_.vbs echo regex.global=%global% 
>>_.vbs echo regEx.IgnoreCase=True 
>>_.vbs echo regex.pattern="%~3" 
>>_.vbs echo wscript.stdOut.write %s% 
cscript /nologo _.vbs <"%~1" >"%~2" 
del _.vbs 
2

顯然您正在搜索「中扮演」並在後面加上一個空格,雖然你的問題是有點含糊。

見我hybrid JScript/batch utility called REPL.BAT,做正則表達式搜索和替換。它適用於從XP任何現代版本的Windows的開始,它不需要安裝任何第三方executeables的。

使用REPL.BAT:

type "yourFile.txt"|repl "played ...." "played INFA" >"yourFile.txt.new" 
move /y "yourFile.txt.new" "yourFile.txt" >nul 
5
@echo off &setlocal 
set "search=search string" 
set "replace=kordo anstataui" 
set "textfile=file.txt" 
set "newfile=new.txt" 

(for /f "delims=" %%i in ('findstr /n "^" "%textfile%"') do (
    set "line=%%i" 
    setlocal enabledelayedexpansion 
    set "line=!line:%search%=%replace%!" 
    echo(!line! 
    endlocal 
))>"%newfile%" 
type "%newfile%" 
+0

我調整了「search = text to find」(包括空格),我也調整了「replace =」(意思就是沒有),我在「newfile」中找到了「找到文本」的完美結果。謝謝你,先生。 –

1
@echo off 
setlocal EnableDelayedExpansion 

set "search=played " 
set replacement=INFA 
set numChars=4 

set line=the mad fox jumped of the old vine and played soccer. 

rem The characters after the equal-signs are Ascii-254 
for /F "tokens=1* delims=■" %%a in ("!line:%search%=■!") do (
    set "rightPart=%%b" 
    set "line=%%a%search%%replacement%!rightPart:~%numChars%!" 
) 

echo !line! 

輸出:

the mad fox jumped of the old vine and played INFAer. 

您必須插入前面的代碼放到一個循環,處理整個文件。我把這部分作爲練習...

相關問題