replacing the whole line based on substring of it.
Moderator: DosItHelp
replacing the whole line based on substring of it.
Hi,
I want to open a file and want to replace a line in the file with a diffrent line, but i don't want to search with full line in the file instead i will give only a part of the line and the program should replace the whole line with given line.
For Example:- In makfi.txt file i have a line like this the current user logged in is ruddet , now i want to use only the current user logged word from this line for my search and i want replace the whole line with a diffrent line.
I have writen below program for my requirement, but it is replacing only a signle word, When i tried with line it not working.
Could some one please help.
@echo off
SETLOCAL=ENABLEDELAYEDEXPANSION
for /f %%a in (makfi.txt) do (
set foo=%%a
if !foo!==ex3 set foo=ex5
echo !foo! >> text.file)
I want to open a file and want to replace a line in the file with a diffrent line, but i don't want to search with full line in the file instead i will give only a part of the line and the program should replace the whole line with given line.
For Example:- In makfi.txt file i have a line like this the current user logged in is ruddet , now i want to use only the current user logged word from this line for my search and i want replace the whole line with a diffrent line.
I have writen below program for my requirement, but it is replacing only a signle word, When i tried with line it not working.
Could some one please help.
@echo off
SETLOCAL=ENABLEDELAYEDEXPANSION
for /f %%a in (makfi.txt) do (
set foo=%%a
if !foo!==ex3 set foo=ex5
echo !foo! >> text.file)
Re: replacing the whole line based on substring of it.
You could try something like that:
Regards
aGerman
Code: Select all
@echo off &setlocal
set "file=makfi.txt"
set "dest=new.txt"
set "search=the current user logged"
set "replace=whatever!"
setlocal enabledelayedexpansion
for /f "delims=:" %%a in ('findstr /nc:"!search!" "!file!"') do (
set "found=!found! %%a"
)
if not defined found goto :eof
for /f "delims=" %%n in ('find /c /v "" !file!') do set "len=%%n"
>"!dest!" (
for /l %%l in (1 1 !len:*: ^=!) do (
set "lin="
set "bool="
set /p "lin="
for %%n in (%found%) do if %%l==%%n set /a bool=1
if defined bool (
echo(!replace!
) else (
echo(!lin!
)
)
)<"!file!"
endlocal
Regards
aGerman
Re: replacing the whole line based on substring of it.
After replacing the text found. Is there a way to write the entire line with the changed word to another file?
example source text
I have a apple - Replace text (apple) with (orange)
example output text
I have a orange - (all words in output file)
Is this possible to do?
Thanks
Ken
example source text
I have a apple - Replace text (apple) with (orange)
example output text
I have a orange - (all words in output file)
Is this possible to do?
Thanks
Ken
Re: replacing the whole line based on substring of it.
Try this: it uses Windows scripting host
"Search and replace.bat" makfi.txt makfi2.txt "apple" "orange"
"Search and replace.bat" makfi.txt makfi2.txt "apple" "orange"
Code: Select all
@echo off
if "%~3"=="" (
echo.Search and replace
echo Syntax:
echo %0 "filein.txt" "fileout.ext" "regex" "replace_text" [first]
echo.
echo. if [first] is present only the first occurrence is changed
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
Re: replacing the whole line based on substring of it.
foxidrive...
Thanks for the respose to this post.
I am learning from this site but I don't understand how your code ties into aGermans example.
Please explain how:
To apply your code using (input file) containing "I have a apple" with (output file) containing "I have a orange"
My goal is:
If the line reads: Today is a good day and I have a apple - with - Today is a good day and I have a orange.
Thank you
Ken
Thanks for the respose to this post.
I am learning from this site but I don't understand how your code ties into aGermans example.
Please explain how:
To apply your code using (input file) containing "I have a apple" with (output file) containing "I have a orange"
My goal is:
If the line reads: Today is a good day and I have a apple - with - Today is a good day and I have a orange.
Thank you
Ken
Re: replacing the whole line based on substring of it.
foxidrive wrote:Try this: it uses Windows scripting host
"Search and replace.bat" makfi.txt makfi2.txt "apple" "orange"
Save the code into a batch file and call it: Search and replace.bat
Then launch the batch file from a cmd window like this:
"Search and replace.bat" "inputfile.txt" "outputfile.txt" "apple" "orange"
Re: replacing the whole line based on substring of it.
Foxidrive...
Very nice!
Thank you
Ken
Very nice!
Thank you
Ken
Re: replacing the whole line based on substring of it.
Foxidrive...
Is there a way to hard code the makfi.txt , maki2.txt , apple and orange then use this code in a already created batch file. Or would it be better to just call search and replace.bat with the parms.
Ken
Is there a way to hard code the makfi.txt , maki2.txt , apple and orange then use this code in a already created batch file. Or would it be better to just call search and replace.bat with the parms.
Ken
Re: replacing the whole line based on substring of it.
Write another batch file:
Code: Select all
@echo off
call "Search and replace.bat" "inputfile.txt" "outputfile.txt" "apple" "orange"
Re: replacing the whole line based on substring of it.
Foxidrive...
This is the only way to use this code by calling it from another batch or from the command line.
I was hoping it could be contained in a aready created batch file without calling a seprate batch.
I can still use it.
Thanks for the code and your help.
Ken
This is the only way to use this code by calling it from another batch or from the command line.
I was hoping it could be contained in a aready created batch file without calling a seprate batch.
I can still use it.
Thanks for the code and your help.
Ken
Re: replacing the whole line based on substring of it.
Ken wrote:Foxidrive...
This is the only way to use this code by calling it from another batch or from the command line.
I was hoping it could be contained in a aready created batch file without calling a seprate batch.
I can still use it.
Thanks for the code and your help.
Ken
If you want to hard code it then replace all the variables %1 %2 %3 %4 with what you would input on the command line.
Code: Select all
set global=true
set s=regex.replace(wscript.stdin.readall,"orange")
>_.vbs echo set regex=new regexp
>>_.vbs echo regex.global=%global%
>>_.vbs echo regEx.IgnoreCase=True
>>_.vbs echo regex.pattern="apple"
>>_.vbs echo wscript.stdOut.write %s%
cscript /nologo _.vbs <"inputfile.txt" >"outputfile.txt"
del _.vbs
Re: replacing the whole line based on substring of it.
Foxidrive...
I see now...this is what I was looking for.
Thank you for your help
Ken
I see now...this is what I was looking for.
Thank you for your help
Ken
Re: replacing the whole line based on substring of it.
Squashman...
I did not pay attention to the previous post.
Thank you for your assistance with the code.
Ken
I did not pay attention to the previous post.
Thank you for your assistance with the code.
Ken