2

I have a to create an autosys job to trigger a batch file which would find a specific string and then replace the next 4 characters.

For example if file (with multiple lines) has below content and i am searching for played

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

I should replace "socc" with "INFA"

I am new to batch files and my lead has been insisting that i do this using a batch file only. Any help would be greatly apprciated.

Thanks, Joy

Joydeep roy
  • 31
  • 1
  • 1
  • 2
  • possible duplicate of [String replacement in batch file](http://stackoverflow.com/questions/2772456/string-replacement-in-batch-file) – jeb Jun 13 '13 at 11:14
  • It is not necessarily SOCC. Any 4 letters which occur after played should be replaced with INFA – Joydeep roy Jun 13 '13 at 11:22
  • Windows command line doesn't have a search and replace tool. Download `sed` or write a shell script. – Endoro Jun 13 '13 at 11:35
  • 1
    You should said that in your question and not in a comment: "I should replace ANY 4 LETTERS WHICH OCCUR AFTER "played" should be replaced with INFA". You should modify your question this way anyway... – Aacini Jun 13 '13 at 17:18

5 Answers5

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%"
Endoro
  • 37,015
  • 8
  • 50
  • 63
  • I adjust "search=text to find " (including space at the end), I also adjust "replace=" (means nothing), I got perfect result that "text to find " is eliminated in the "newfile". Thank you Sir. – Rhak Kahr Oct 08 '14 at 06:15
2

I use this at times: 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
foxidrive
  • 40,353
  • 10
  • 53
  • 68
2

Apparently you are searching for "played " with a space at the end, although your question is a bit vague.

See my hybrid JScript/batch utility called REPL.BAT that does regular expression search and replace. It works on any modern version of Windows from XP onward, and it does not require installation of any 3rd party executeables.

Using REPL.BAT:

type "yourFile.txt"|repl "played ...." "played INFA" >"yourFile.txt.new"
move /y "yourFile.txt.new" "yourFile.txt" >nul
Community
  • 1
  • 1
dbenham
  • 127,446
  • 28
  • 251
  • 390
1

Get yourself a copy of sed for your OS and call it from a batch file.

sed -e 's/socc/INFA/g' inputFileName > outputFileName
Joe
  • 122,218
  • 32
  • 205
  • 338
  • thanx for the suggestion but as per the clind policy i am not allowed to use any external utilities .... – Joydeep roy Jun 13 '13 at 11:21
  • Another alternative is to use powershell or vbscript, which you can call from your batch file. – Joe Jun 13 '13 at 11:27
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!

Output:

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

You must insert previous code into a loop that process the entire file. I left that part as an exercise for you...

Aacini
  • 65,180
  • 12
  • 72
  • 108