Page 1 of 1

Remove characters from text file

Posted: 03 Dec 2018 23:58
by big_caballito
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.

Re: Remove characters from text file

Posted: 04 Dec 2018 12:26
by big_caballito
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

Re: Remove characters from text file

Posted: 04 Dec 2018 12:47
by aGerman
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

Re: Remove characters from text file

Posted: 04 Dec 2018 12:49
by Ed Dyreen
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.

Re: Remove characters from text file

Posted: 04 Dec 2018 13:04
by big_caballito
Thanks, you two