Solved Remove line numbers in a text file using a batch file

February 13, 2018 at 03:06:42
Specs: Windows
Hello, I would like to remove the line numbers in a file using Dos batch file. My file is like this :

TITLE TEST

PART 1 FIRST TEST
-------------------------------.

1 : AAA
2 : BBB
3 : CCC
4 : DDD
5 : EEE
6 : FFF
7 : GGG
8 : HHH
9 : LLL
10 : MMM
11 : NNN

This is how it should be

TITLE TEST

PART 1 FIRST TEST
-------------------------------.

: AAA
: BBB
: CCC
: DDD
: EEE
: FFF
: GGG
: HHH
: LLL
: MMM
: NNN

I don't want to use or install any other component but just using Dos. Note that I maybe also need wildcards because the lines numbers are from 1 to 999 and 1 is not 001 but space-space-1
Hope it is clear, thank you very much in advance.


See More: Remove line numbers in a text file using a batch file

Report •

✔ Best Answer
February 14, 2018 at 19:32:45
Ok, we're kind of used to this. Here's to round 4:

@echo off
(for /f "tokens=1,2* delims=[] " %%a in ('find /n /v ""^<test1') do (
if "%%c" equ "" (echo.%%b) else (echo.%%c)
))>test2

If it gets more complicated, you may have to look into hakoo's and hide's approach using findstr, (although findstr is feeble at reg-ex and difficult to work with, imo).

message edited by nbrane



#1
February 13, 2018 at 03:40:34
test.bat:

@echo off
echo off>part2.txt
for /f "tokens=1,* delims=:" %%a in (part1.txt) do echo :%%b>>part2.txt

i5-6600K[delid]@4.814GHz/4.613GHz cache@1.38v | 2x4GB Crucial-DDR4-2133@14-14-14-28 1T 2808MHz@1.37v
ASUS Z170K
Samsung 250GB SSD 850 EVO
MSI Armor RX 570 4GB@1380c/2087m BiosMod
VS450

message edited by hidde663


Report •

#2
February 13, 2018 at 04:10:23
Thank you very much hidde663, when I execute the code I get error : b not expected

EDIT : I changed to %%a and %%b and now the code runs, but I get lines without number deleted or cut

Thanks

message edited by Gibernaus


Report •

#3
February 13, 2018 at 05:20:51
OH yeah, sorry, ive only tested the code in cmd which only forces me to use %a instead of %%a.

im getting data in part2.txt:

: AAA
: BBB
: CCC
: DDD
: EEE
: FFF
: GGG
: HHH
: LLL
: MMM
: NNN

btw, i can also remove the ":" if you want

i5-6600K[delid]@4.814GHz/4.613GHz cache@1.38v | 2x4GB Crucial-DDR4-2133@14-14-14-28 1T 2808MHz@1.37v
ASUS Z170K
Samsung 250GB SSD 850 EVO
MSI Armor RX 570 4GB@1380c/2087m BiosMod
VS450

message edited by hidde663


Report •

Related Solutions

#4
February 13, 2018 at 05:45:43
Ok, thank you very much
Now the code is :

@echo off
echo off>part2.txt
for /f "tokens=1,* delims=: " %%a in (part1.txt) do echo :%%b>>part2.txt

it runs but starting from :

TITLE TEST

PART 1 FIRST TEST
-------------------------------

1 : AAA
2 : BBB
3 : CCC
4 : DDD
5 : EEE
6 : FFF
7 : GGG
8 : HHH
9 : LLL
10 : MMM
11 : NNN

I get

:TEST
:1 FIRST TEST
:
:AAA
:BBB
:CCC
:DDD
:EEE
:FFF
:GGG
:HHH
:LLL
:MMM
:NNN

so I've lost some parts of the textes that I need not to modify. It should be as follow :

TITLE TEST

PART 1 FIRST TEST
-------------------------------

:AAA
:BBB
:CCC
:DDD
:EEE
:FFF
:GGG
:HHH
:LLL
:MMM
:NNN


Thank you again for your support

message edited by Gibernaus


Report •

#5
February 13, 2018 at 06:35:12
i did not think i had to include this part:
TITLE TEST

PART 1 FIRST TEST

tho i can modify it so it will work as you sugested, give me a few minutes

i5-6600K[delid]@4.814GHz/4.613GHz cache@1.38v | 2x4GB Crucial-DDR4-2133@14-14-14-28 1T 2808MHz@1.37v
ASUS Z170K
Samsung 250GB SSD 850 EVO
MSI Armor RX 570 4GB@1380c/2087m BiosMod
VS450


Report •

#6
February 13, 2018 at 06:57:01
No problem, take your time, thanks

Report •

#7
February 13, 2018 at 07:42:54
note that there is an extra set/a startofnumbers-=2
this is to compensate for the fact that type/more can not process empty lines
add more to the value of startofnumbers if you want to compensate for more empty lines in your paticular header.

for some reason i can not find an easy workaround for this problem, anyways the below .bat file gives me these results:

TITLE TEST
PART 1 FIRST TEST
-------------------------------.

AAA
BBB
CCC
DDD
EEE
FFF
GGG
HHH
LLL
MMM
NNN


::----start of test.bat----

@echo on&setlocal enabledelayedexpansion
::make part2 empty
echo off>part2.txt
::find firstline of numbers
set startofnumbers=0
for /f "tokens=1 delims=[]" %%a in ('type "Part1.txt" ^| find /N "1 : "') do set startofnumbers=%%a&goto next
:next
::
set/a startofnumbers-=1
::this line below is to compensate for lost empty lines....., have not found a workaround yet
set/a startofnumbers-=2
set/a linecount=0
for /F "tokens=* delims=" %%a in (Part1.txt) do (
if !linecount! GEQ !startofnumbers! goto next1
echo %%a>>part2.txt
set/a linecount+=1
)
:next1
for /f "tokens=1,* delims=: " %%a in ('more "Part1.txt" +!startofnumbers!') do if "%%b"=="" (echo. >>part2.txt) else echo %%b>>part2.txt
start notepad.exe "part2.txt"
exit/b

::----end of test.bat----

i5-6600K[delid]@4.814GHz/4.613GHz cache@1.38v | 2x4GB Crucial-DDR4-2133@14-14-14-28 1T 2808MHz@1.37v
ASUS Z170K
Samsung 250GB SSD 850 EVO
MSI Armor RX 570 4GB@1380c/2087m BiosMod
VS450

message edited by hidde663


Report •

#8
February 13, 2018 at 07:50:55
You've done a lot of work, you are very kind !
I will try and I will let you know !
Thank you so much.

Report •

#9
February 13, 2018 at 16:07:42
Just give a try for this batch file by drag and drop your input file over this script to get the ouput file as Final.txt.

@echo off
Set "InputFile=%~1"
If "%InputFile%" == "" (
	echo Just drag and drop your input file over this script "%~nx0"
	Timeout /T 4 /nobreak>nul & exit
)
Set "OutputFile=Final.txt"
If exist 4.txt Del 4.txt
Findstr /v /c:":" "%InputFile%">"%OutputFile%"
Findstr /R /c:"[0-9]" "%InputFile%">2.txt
Findstr /V /c:"PART" "2.txt">3.txt
for /f "tokens=1,* delims=:" %%a in ('Type "3.txt"') do echo :%%b>>4.txt
Copy /b "%OutputFile%" + /b 4.txt>nul
Del 4.txt & Del 3.txt & Del 2.txt
Start "" "%OutputFile%"

message edited by Hackoo


Report •

#10
February 13, 2018 at 21:14:28
Looks kind of complicated to me... This seemed to work except for the blank lines requirement:
@echo off
(for /f "tokens=1* delims=:" %%a in (text1) do (
if "%%b" equ "" (echo %%a) else (echo :%%b)
))>text2

To handle blank lines with batch, the "traditional" method is to use find /n /v as in following:
@echo off
for /f "tokens=1,2* delims=:[]" %%a in ('find /n /v ""^<text1') do (
if "%%c" equ "" (echo.%%b) else (echo.:%%c)
)


Report •

#11
February 14, 2018 at 01:33:50
Thank you very much to hidde663, Hackoo and nbrane, your code works almost as I would like.
At the moment the one posted by nbrane is able to manage even the empty lines.
One last thing, would it be possible to limit the area to test the presence of line numbers only for the first 3 columns starting from the left? The text file I attached at the beginning was not complete, it has also an instruction like this

11 Z001 : CCC
12 : DDD
13 : VVV
14 Z002 : NNN
15 : FFF
16 : SSS
----

after processing it lost strings Z001 and Z002

: CCC
: DDD
: VVV
: NNN
: FFF
: SSS
----

I wish it were like that

Z001 : CCC
: DDD
: VVV
Z002 : NNN
: FFF
: SSS
----

Thanks a lot, I really appreciate your help

message edited by Gibernaus


Report •

#12
February 14, 2018 at 19:32:45
✔ Best Answer
Ok, we're kind of used to this. Here's to round 4:

@echo off
(for /f "tokens=1,2* delims=[] " %%a in ('find /n /v ""^<test1') do (
if "%%c" equ "" (echo.%%b) else (echo.%%c)
))>test2

If it gets more complicated, you may have to look into hakoo's and hide's approach using findstr, (although findstr is feeble at reg-ex and difficult to work with, imo).

message edited by nbrane


Report •

#13
February 14, 2018 at 23:23:07
Thanks a lot nbrane, that's ok, it works !
Many thanks also to hidde663 and Hackoo for your support.
Best regards, Gib..

Report •

Ask Question