replacing the whole line based on substring of it.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
pt88john
Posts: 1
Joined: 30 Sep 2011 06:36

replacing the whole line based on substring of it.

#1 Post by pt88john » 30 Sep 2011 09:41

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)

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: replacing the whole line based on substring of it.

#2 Post by aGerman » 01 Oct 2011 06:03

You could try something like that:

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

Ken
Posts: 34
Joined: 09 Dec 2009 13:47

Re: replacing the whole line based on substring of it.

#3 Post by Ken » 04 Feb 2012 12:57

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: replacing the whole line based on substring of it.

#4 Post by foxidrive » 10 Feb 2012 07:01

Try this: it uses Windows scripting host

"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

Ken
Posts: 34
Joined: 09 Dec 2009 13:47

Re: replacing the whole line based on substring of it.

#5 Post by Ken » 11 Feb 2012 06:55

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: replacing the whole line based on substring of it.

#6 Post by foxidrive » 11 Feb 2012 07:48

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"

Ken
Posts: 34
Joined: 09 Dec 2009 13:47

Re: replacing the whole line based on substring of it.

#7 Post by Ken » 11 Feb 2012 08:13

Foxidrive...

Very nice!

Thank you
Ken

Ken
Posts: 34
Joined: 09 Dec 2009 13:47

Re: replacing the whole line based on substring of it.

#8 Post by Ken » 11 Feb 2012 08:37

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: replacing the whole line based on substring of it.

#9 Post by foxidrive » 11 Feb 2012 23:17

Write another batch file:

Code: Select all

@echo off
call "Search and replace.bat" "inputfile.txt" "outputfile.txt" "apple" "orange"

Ken
Posts: 34
Joined: 09 Dec 2009 13:47

Re: replacing the whole line based on substring of it.

#10 Post by Ken » 12 Feb 2012 06:09

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

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: replacing the whole line based on substring of it.

#11 Post by Squashman » 12 Feb 2012 12:41

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

Ken
Posts: 34
Joined: 09 Dec 2009 13:47

Re: replacing the whole line based on substring of it.

#12 Post by Ken » 13 Feb 2012 03:17

Foxidrive...

I see now...this is what I was looking for.

Thank you for your help
Ken

Ken
Posts: 34
Joined: 09 Dec 2009 13:47

Re: replacing the whole line based on substring of it.

#13 Post by Ken » 13 Feb 2012 03:34

Squashman...

I did not pay attention to the previous post.

Thank you for your assistance with the code.

Ken

Post Reply