Page 1 of 1
Bat to Find Words in .txt And Delete all after it
Posted: 04 Apr 2013 12:00
by daillest319
I need to delete everything after the word "END OF TRANSFER" it can be anywhere in the text file. Currently I only know how to delete the line which has "END OF TRANSFER" but even this doesn't work the way i want it. END OF TRANSFER coubld be anywhere in the file and may not always be the first word in the line.
here what i have...
Code: Select all
@echo off
type "C:\Temp\HSTY.txt" | findstr /r /v /c:"^End of Transfer" >> "C:\Temp\Temp.log"
Re: Bat to Find Words in .txt File And Delete all after it
Posted: 04 Apr 2013 12:59
by Endoro
Hi,
you can try this:
Code: Select all
@echo off &setlocal
set "infile=C:\Temp\HSTY.txt"
set "outfile=C:\Temp\Temp.log"
set "search=End of Transfer"
set "found="
del "%outfile%" >nul 2>&1
for /f "tokens=1*delims=:" %%i in ('type "%infile%" ^| findstr /n "^"') do (
if not defined found echo(%%j>>%outfile%
if /i "%%j"=="%search%" set "found=TRUE"
)
endlocal
Re: Bat to Find Words in .txt File And Delete all after it
Posted: 04 Apr 2013 13:07
by daillest319
Works but i noticed if there's a word or anythign in front of END OF TRANSFER it will not work is there a way to pick up the word anywhere even if something in front of it?
Re: Bat to Find Words in .txt File And Delete all after it
Posted: 04 Apr 2013 15:33
by Endoro
OK, try this:
Code: Select all
@echo off &setlocal
set "infile=C:\Temp\HSTY.txt"
set "outfile=C:\Temp\Temp.log"
set "search=End of Transfer"
set "found="
del "%outfile%" 2>nul
for /f "delims=:" %%i in ('type "%infile%" ^|findstr /inc:"%search%"') do if not defined found set /a found=%%i
if not defined found echo(%search% not found in %infile% &goto:eof
for /f "tokens=1*delims=:" %%i in ('type "%infile%" ^|findstr /n "^"') do if %%i leq %found% echo(%%j>>"%outfile%"
endlocal
Re: Bat to Find Words in .txt File And Delete all after it
Posted: 04 Apr 2013 17:57
by foxidrive
daillest319 wrote:I need to delete everything after the word "END OF TRANSFER"
Does that mean everything after it on the line, or including every line that comes after it too.
Re: Bat to Find Words in .txt File And Delete all after it
Posted: 04 Apr 2013 23:36
by Endoro
Yes, good remark. My code puts the whole line with "END OF TRANSFER" to the output file.
Re: Bat to Find Words in .txt File And Delete all after it
Posted: 05 Apr 2013 01:27
by Endoro
And here we go again:
Code: Select all
@echo off &setlocal
set "infile=C:\Temp\HSTY.txt"
set "outfile=C:\Temp\Temp.log"
set "search=End of Transfer"
set "found="
del "%outfile%" 2>nul
for /f "delims=:" %%i in ('type "%infile%" ^|findstr /inc:"%search%"') do if not defined found set /a found=%%i
if not defined found echo(%search% not found in %infile% &goto:eof
for /f "tokens=1*delims=:" %%i in ('type "%infile%" ^|findstr /n "^"') do (
if %%i lss %found% (
echo(%%j
) else if %%i equ %found% (
set "lastline=%%j"
setlocal enabledelayedexpansion
set "right=!lastline:*%search%=!"
call set "lastline=%%lastline:%search%!right!=%search%%%"
echo(!lastline!
endlocal
)
)>>"%outfile%"
endlocal
Hard work for !lastline!, more than all other lines together ..

Re: Bat to Find Words in .txt And Delete all after it
Posted: 05 Apr 2013 11:39
by daillest319
to foxidrive - i need to delete every line that comes after it so i'm keeping everything thats on End of Transfer line.
I tried both codes and they workbut they run very slow on big text files is there anyway to speed it up or different method that runs faster?
Re: Bat to Find Words in .txt And Delete all after it
Posted: 05 Apr 2013 17:37
by foxidrive
This uses GNUsed and should be pretty swift.
Code: Select all
@echo off
for /f "delims=:" %%a in ('findstr /n "End of Transfer" ^<test.txt') do set num=%%a
sed 1,%num%!d test.txt
pause
Re: Bat to Find Words in .txt And Delete all after it
Posted: 07 Apr 2013 10:12
by dbenham
@foxidrive - I believe your sed solution will preserve trailing text after "End of Transfer" that appears on the same line. I imagine it would not be too difficult to truncate the line after "End of Transfer".
Here is a solution that uses my
REPL.BAT regex search and replace utility.
Code: Select all
@echo off
setlocal
set "file=test.txt"
type "%file%" | repl "(End of Transfer)(.*\n?)*" "$1" m >"%file%.mod"
move /q "%file%.mod" "%file%"
Dave Benham
Re: Bat to Find Words in .txt And Delete all after it
Posted: 08 Apr 2013 03:34
by foxidrive
dbenham wrote:@foxidrive - I believe your sed solution will preserve trailing text after "End of Transfer" that appears on the same line. I imagine it would not be too difficult to truncate the line after "End of Transfer".
Yes Dave, his last post said that he wanted all the text on the same line.
Re: Bat to Find Words in .txt And Delete all after it
Posted: 08 Apr 2013 22:44
by dbenham
foxidrive wrote:dbenham wrote:@foxidrive - I believe your sed solution will preserve trailing text after "End of Transfer" that appears on the same line. I imagine it would not be too difficult to truncate the line after "End of Transfer".
Yes Dave, his last post said that he wanted all the text on the same line.
Ahh... I missed that nugget of info.
My solution needs to be edited a bit in light of that info:
Code: Select all
@echo off
setlocal
set "file=test.txt"
type "%file%" | repl "(End of Transfer.*\n?)(.*\n?)*" "$1" m >"%file%.mod"
move /q "%file%.mod" "%file%"
Dave Benham