Modify an existing line in a text file

September 7, 2010 at 10:20:39
Specs: Windows XP
I need to write a batch file which will look at a file and look for a line that starts with "ABC". If that line exists then delete the entire line and replace it with a new line. Is this possible?

See More: Modify an existing line in a text file

Report •

#1
September 8, 2010 at 02:36:27
@echo off > newfile & setLocal enableDELAYedeXpansion

findstr /B /C:ABC myfile > nul
if errorlevel 1 echo no && goto :eof

for /f "tokens=1 delims=:" %%a in ('findstr /n /B /C:ABC myfile') do (
set L=%%a
)

set N=
for /f "tokens=1* delims=[]" %%i in ('find /v /n "" ^< myfile') do (
set /a N+=1
set S=%%j
if !N! equ !L! set S=NEWLINEOFTXT
>> newfile echo.!S!
)


=====================================
Helping others achieve escape felicity

M2


Report •

#2
September 8, 2010 at 06:35:13
download sed for windows then do this

c:\test> sed -i.bak "/^[ \t]*ABC/s/.*/replacement line/" file

GNU win32 packages | Gawk


Report •

#3
September 8, 2010 at 06:38:07
@M2, i see you used find/findstr a lot. Is there a need to do that? I mean, just a for loop with findstr will do right?

GNU win32 packages | Gawk


Report •

Related Solutions

#4
September 8, 2010 at 07:53:30
Gees, this is the best I could do with batch, but it does deal with
!'s and lines that start with ] or [....


yourfile == the file name
newfile == the temp file name
str == lines that start with this will be replaced
rep == the replacement line


@echo off
SetLocal DisableDelayedExpansion


rem Double quote file names if needed
rem you can specify a full path


set yourfile="yourfile"

rem "%newfile%" will be overwritten
set newfile="newfile"
copy nul %newfile% > nul



rem Use carets to escape characters if needed below

set str=ABC
set rep=this is the replacement line, can be nothing (case sensitive)


for /f %%a in ('cmd /u /c set /p "=%str:"= %"^<nul ^| find /c /v "" ') do set len=%%a


for /f "delims=" %%a in (' find /v /n "" ^< %yourfile% ') do (
    set line=%%a
    SetLocal EnableDelayedExpansion
    set line=!line:*]=!
    if defined line (
        if "!line:~0,%len%!"=="!str!" (
            >> !newfile! echo.!rep!
        ) else >> !newfile! echo !line!
    ) else >> !newfile! echo.
    endlocal
)

echo If you are prompted you will loose either the old or current
echo backup, you can manually back up before selecting 
copy /-y %yourfile% "%yourfile:"=%.bak"

move %newfile% %yourfile%
pause


Report •

#5
September 8, 2010 at 10:39:28
Thanks a lot for your help! Judago your script almost works it just keeps the original line and appends the new line to the end. I don't care that the line is at the end but I need the old line to be deleted.


Report •

#6
September 8, 2010 at 11:45:35
That's not happening on my end can you post the entire script that this is happening with(I really just want to see the changes...)?

Report •

#7
September 8, 2010 at 12:09:34
Thanks. I figured it out I copied it wrong somehow, not sure what happened but i recopied it and it works now. Although I was wondering if you could help me with one more thing. This is to modify a config file and the lines actually start with <tab>ABC. The script doesn't seem to work with that, is there a way to have it look for the <tab> or just empty space on the line before the ABC?

Thanks again for all your help!!


Report •

#8
September 8, 2010 at 12:20:49
Works with tabs and spaces for me:

set str=	ABC


It may be that your editor either doesn't support tabs or is set to insert n spaces instead.

Instead of hitting tab try this (assuming 101/10x english keyboard - don't know about others):

With the Numlock on, Hold down alt and on the numeric keypad(the right of the keyboard) hit "0" then "9", then release alt.


Report •

#9
September 8, 2010 at 12:38:06
Its still not working, editing in notepad

Script:
@echo off
SetLocal DisableDelayedExpansion


rem Double quote file names if needed
rem you can specify a full path


set yourfile=c:\test.txt

rem "%newfile%" will be overwritten
set newfile=c:\test2.txt
copy nul %newfile% > nul

rem Use carets to escape characters if needed below

set str= ABC
set rep=123


for /f %%a in ('cmd /u /c set /p "=%str:"= %"^<nul ^| find /c /v "" ') do set len=%%a


for /f "delims=" %%a in (' find /v /n "" ^< %yourfile% ') do (
set line=%%a
SetLocal EnableDelayedExpansion
set line=!line:*]=!
if defined line (
if "!line:~0,%len%!"=="!str!" (
>> !newfile! echo.!rep!
) else >> !newfile! echo !line!
) else >> !newfile! echo.
endlocal
)

echo If you are prompted you will loose either the old or current
echo backup, you can manually back up before selecting
REM copy /-y %yourfile% "%yourfile:"=%.bak"

REM move %newfile% %yourfile%
pause

test.txt:

ABC

If I remove the <tab> from both test.txt and the script it works just fine. If the <tab> is there it doesn't work. I have no idea what is wrong. Thanks for helping, I really appreciate it.


Report •

#10
September 8, 2010 at 12:50:39

set str= ABC

That's not a tab, in fact it's two characters.

It's hard to tell because of the forum, but from the page source it looks like you have 8 spaces in test.txt, not a tab.


Report •

#11
September 8, 2010 at 12:56:10
yes in the post i put it in as 8 spaces because hitting tab moved out of the text block. In test.txt it is actually a <tab>.

test.txt:
<tab>ABC

the two strings are identical so i can't see why it doesn't work but when I check test2.txt it is the same as the original, it did not replace the string. It might be something on my end I'm not sure.


Report •

#12
September 8, 2010 at 12:57:17
Here is a replacement for the main loop that will ignore all leading spaces and tabs of the input file:


for /f "delims=" %%a in (' find /v /n "" ^< %yourfile% ') do (
    set line=%%a
    SetLocal EnableDelayedExpansion
    set line=!line:*]=!
    if defined line (
        for /f "tokens=*" %%b in ("!line!") do set blankskip=%%b
	if defined blankskip (
            if "!blankskip:~0,%len%!"=="!str!" (
                >> !newfile! echo.!rep!
            ) else >> !newfile! echo !line!
        ) else >> !newfile! echo !line!
    ) else >> !newfile! echo.
    endlocal
)

I think it should be fairly obvious where to splice it in.


Report •

#13
September 8, 2010 at 13:01:25
You posted just before me ;)

All I can say is that it works fine to replace spaces and tabs for me, so I'm not sure what the problem is....


Report •

#14
September 8, 2010 at 13:02:09
Thank you so much that worked perfectly!!!

Report •

Ask Question