Bat to Find Words in .txt And Delete all after it

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
daillest319
Posts: 27
Joined: 31 Jan 2012 14:45

Bat to Find Words in .txt And Delete all after it

#1 Post by daillest319 » 04 Apr 2013 12:00

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"

Last edited by daillest319 on 05 Apr 2013 11:35, edited 2 times in total.

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: Bat to Find Words in .txt File And Delete all after it

#2 Post by Endoro » 04 Apr 2013 12:59

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


daillest319
Posts: 27
Joined: 31 Jan 2012 14:45

Re: Bat to Find Words in .txt File And Delete all after it

#3 Post by daillest319 » 04 Apr 2013 13:07

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?

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: Bat to Find Words in .txt File And Delete all after it

#4 Post by Endoro » 04 Apr 2013 15:33

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


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

Re: Bat to Find Words in .txt File And Delete all after it

#5 Post by foxidrive » 04 Apr 2013 17:57

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.

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: Bat to Find Words in .txt File And Delete all after it

#6 Post by Endoro » 04 Apr 2013 23:36

Yes, good remark. My code puts the whole line with "END OF TRANSFER" to the output file.

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: Bat to Find Words in .txt File And Delete all after it

#7 Post by Endoro » 05 Apr 2013 01:27

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 .. :mrgreen:

daillest319
Posts: 27
Joined: 31 Jan 2012 14:45

Re: Bat to Find Words in .txt And Delete all after it

#8 Post by daillest319 » 05 Apr 2013 11:39

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?

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

Re: Bat to Find Words in .txt And Delete all after it

#9 Post by foxidrive » 05 Apr 2013 17:37

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

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Bat to Find Words in .txt And Delete all after it

#10 Post by dbenham » 07 Apr 2013 10:12

@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

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

Re: Bat to Find Words in .txt And Delete all after it

#11 Post by foxidrive » 08 Apr 2013 03:34

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.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Bat to Find Words in .txt And Delete all after it

#12 Post by dbenham » 08 Apr 2013 22:44

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

Post Reply