2

How to create a batch file that deletes last two characters from every line of a txt file named list.txt

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user2677945
  • 21
  • 1
  • 2

4 Answers4

3
@echo off &setlocal
(for /f "delims=" %%a in (list.txt) do (
    set "line=%%a"
    setlocal enabledelayedexpansion
    set "line=!line:~0,-2!"
    echo(!line!
    endlocal
))>list.txt.new

you can also use sed for Windows:

sed "s/..$//" list.txt>list.txt.new
Endoro
  • 37,015
  • 8
  • 50
  • 63
2

Using a hybrid JScript/batch utility called REPL.BAT

type list.txt | repl ..$ "" >list.txt.new

If you want to over-write the original, then add this command

move /y list.txt.new list.txt >nul
Community
  • 1
  • 1
dbenham
  • 127,446
  • 28
  • 251
  • 390
0

Basically you take a substring of the string you read.

set str=politic
echo.%str%
set str=%str:~0,4%
echo.%str%

Since you cannot overwrite the file itself just make a copy of the original file. Write the new strings in there using the above method to change them. Then replace the old file with the new one. For details about that look at the following link Batch / Find And Edit Lines in TXT file

Community
  • 1
  • 1
DrkNess
  • 676
  • 5
  • 4
0

I was just trying to do this but with a cut implementation. I add it here in case someone also wants it.

rev list.txt | cut --complement  -c -2 | rev >> new_list.txt 
JoseleMG
  • 302
  • 5
  • 18