How to create a batch file that deletes last two characters from every line of a txt file named list.txt
Asked
Active
Viewed 3,901 times
4 Answers
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
-
Do you get a commission from sed? ;-) – LS_ᴅᴇᴠ Aug 30 '13 at 09:11
-
@LS_dev I like it: it is reliable and widely used. :) – Endoro Aug 30 '13 at 09:14
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
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