Remove characters from text file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
big_caballito
Posts: 3
Joined: 23 Oct 2018 08:18

Remove characters from text file

#1 Post by big_caballito » 03 Dec 2018 23:58

Hello, i need to remove the first 8 characters, as well as the last 20 characters of each line in a text file, regardless of what the characters are.
I'm fine with using JREPL or some other utility like that to accomplish this.

big_caballito
Posts: 3
Joined: 23 Oct 2018 08:18

Re: Remove characters from text file

#2 Post by big_caballito » 04 Dec 2018 12:26

I solved my own problem, here is the code:

Code: Select all

for /f "tokens=*" %%D in (example.txt) do (
	set texte=%%D
	set texte=!texte:~8!
	set texte=!texte:~0,-19!
	echo "!texte:  = !" >> temp.txt
)
type temp.txt > example.txt
del temp.txt

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

Re: Remove characters from text file

#3 Post by aGerman » 04 Dec 2018 12:47

Your task should still work with pure Batch as long as the line lengths don't exceed 1021 characters.

Code: Select all

@echo off &setlocal
set "txt=test.txt"

setlocal EnableDelayedExpansion
<"!txt!" >"!txt!.~tmp" (
  for /f %%i in ('type "!txt!"^|find /c /v ""') do for /l %%j in (1 1 %%i) do (
    set "ln=" &set /p "ln="
    if not defined ln (
      echo(
    ) else (
      echo(!ln:~8,-20!
    )
  )
)

move /y "!txt!.~tmp" "!txt!"
endlocal
Steffen

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Remove characters from text file

#4 Post by Ed Dyreen » 04 Dec 2018 12:49

big_caballito wrote:
04 Dec 2018 12:26
I solved my own problem, here is the code:

Code: Select all

for /f "tokens=*" %%D in (example.txt) do (
	set texte=%%D
	set texte=!texte:~8!
	set texte=!texte:~0,-19!
	echo "!texte:  = !" >> temp.txt
)
type temp.txt > example.txt
del temp.txt

Code: Select all

for /F delims^=^ eol^= %%r in (example.txt) do (
	set "texte=%%r"
	set "texte=!texte:~8,-19!"
	>>temp.txt (echo("!texte:  = !")
)
type temp.txt > example.txt
del temp.txt
Same code with delims set to nothing and eol char also nothing, so for only splits on lineFeed. tokens is irrelevant parameter because you are trying to retrieve the entire line in a single token. set assignment with quotes prevent accidental spaces sneaking their way into variables. echo sometimes fails better use echo( and surrounded with braces for same reason prevent accidental spaces sneaking their way into variables.

several topics on all of the above.

big_caballito
Posts: 3
Joined: 23 Oct 2018 08:18

Re: Remove characters from text file

#5 Post by big_caballito » 04 Dec 2018 13:04

Thanks, you two

Post Reply