Delete a specific line in a text file

May 1, 2009 at 22:34:11
Specs: Windows XP
I have a text file with line numbers length of 6 at the beginning of each lines,
for example:
000001:abc
000002:def
000003:ghi
...
001000:xyz
I just want to use a windows bat to have my lines from 10 to 2375 in the output, how could i do?

i have tried to use
setlocal enabledelayedexpansion
set StartLine=2375
set EndLine=10
for /f "tokens=* delims= " %%c in (outfile2) do (
set str=%%c
set LineNum=!str:~0,6!
if !LineNum! LEQ !StartLine! (
if !LineNum! GEQ !EndLine! (
echo !LineNum!
)
)
)
but it will shift 2 lines, which is from 12 to 2377.


See More: Delete a specific line in a text file

Report •


#1
May 2, 2009 at 01:25:04
:: get lines from x to y

@echo off > newfile & setLocal EnableDelayedExpansion
set StartLine=002375
set EndLine=000010
for /f "tokens=* delims= " %%c in (outfile2) do (
set str=%%c
set LineNum=!str:~0,6!
if !LineNum! LEQ !StartLine! if !LineNum! GEQ !EndLine! (
echo !LineNum! >> newfile
)
)


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#2
May 2, 2009 at 01:43:40
vbscript
Set objFS = CreateObject("Scripting.FileSystemObject")
strFile = "c:\test\file.txt"
Set objFile = objFS.OpenTextFile(strFile)
Do Until objFile.AtEndOfLine
	linenum = objFile.Line
	strLine = objFile.ReadLine
	If linenum >= 10 and linenumber <= 2000 Then		
		WScript.Echo strLine
	End If 
Loop

save the above as myscript.vbs and on command prompt

c:\test> cscript /nologo myscript.vbs 

redirect to new file as required. Note the script doesn't check the first 6 chars, but assumed that the 6 char numbers correspond to exact line numbers starting from 1

Another alternative, if you have programming tools like Perl, just one line will do...eg

c:\test> perl -ne "print if $.>=10 && $.<=2000" file.txt

Unix Win32 tools | Gawk for Windows


Report •

Related Solutions


Ask Question