Find a string and replace specific letters in batch file
Asked Answered
E

5

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

Embolic answered 13/6, 2013 at 11:12 Comment(4)
possible duplicate of String replacement in batch fileSmoothbore
It is not necessarily SOCC. Any 4 letters which occur after played should be replaced with INFAEmbolic
Windows command line doesn't have a search and replace tool. Download sed or write a shell script.Bonnibelle
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...Buke
B
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%"
Bonnibelle answered 13/6, 2013 at 12:18 Comment(1)
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.Assimilation
B
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
Belabor answered 13/6, 2013 at 12:2 Comment(0)
F
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
Freemon answered 13/6, 2013 at 12:8 Comment(0)
S
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
Synaesthesia answered 13/6, 2013 at 11:19 Comment(2)
thanx for the suggestion but as per the clind policy i am not allowed to use any external utilities ....Embolic
Another alternative is to use powershell or vbscript, which you can call from your batch file.Synaesthesia
B
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...

Buke answered 13/6, 2013 at 17:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.