Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Delete first line of .TXT file?

3,671 views
Skip to first unread message

Rick Charnes

unread,
May 22, 2006, 5:08:06 PM5/22/06
to
Can anyone think of an easy way in a batch file to delete the first line
in a 2000-line text file, and save the modified file to a new name?
Thanks.

Harlan Grove

unread,
May 22, 2006, 5:41:11 PM5/22/06
to
Rick Charnes wrote...

>Can anyone think of an easy way in a batch file to delete the first line
>in a 2000-line text file, and save the modified file to a new name?

more /E +1 YourFilenameHere > YourFilenameHere.modified

zackrspv

unread,
May 22, 2006, 6:48:39 PM5/22/06
to
>>Can anyone think of an easy way in a batch file to delete the first line
in a 2000-line text file, and save the modified file to a new name?
Thanks.
>>more /E +1 YourFilenameHere > YourFilenameHere.modified

Alternatively, this batch below will skip however many lines the user
wants in whatever file they want, exportd to whatever file they want.

Tested on Windows XP SP2.

New lines start with a line number, wrapped lines do not, merge wraped
lines onto previous line and remove line numbers before running script.

===Script: LN.cmd===
01. @echo off
02. cls
03. if "%1" NEQ "" if "%2" NEQ "" if "%3" NEQ "" GOTO :go
04. echo SYNTAX: ln ## FILENAME FILENAME2
05. echo --------------------------------
06. echo ## -- # of lines to skip
07. echo FILENAME -- File to Look Into
08. echo FILENAME2 -- File to Export Too
09. echo/
10. goto :eof
11.
12. :go
13. echo/
14. echo/
15. if NOT EXIST %2 (echo %2 -- Does not exist, try again! && goto
:eof)
16. echo Processing %2, skipping %1 lines and exporting to %3...
17. for /f "tokens=* Skip=%1" %%a in (%2) do echo %%a>>%3
===End: LN.cmd===

How it works:

Line 15 goes out and checks to see if the 'host' file exists, if not,
errors out and quits.
Line 17 is broken down as thus:

for /f "tokens=*
---Tells the script to include everything within the file

skip=%1
---Number of lines to skip at the start of the file

%%a in (%2)
---The Host file and export variable.

do echo %%a >> %3
--- echo's all the data after the # of skiped lines to %3, the output
file.

Hope that helps!

zackrspv

unread,
May 22, 2006, 6:57:01 PM5/22/06
to
Also, if you want to see if the # of lines the user wants to skip is
too much, you can include that inside of the batch as well:

*Watch for wrapped lines!*

===Script: LN.cmd===
01. ::@echo off
02. cls
03. if "%1" NEQ "" if "%2" NEQ "" if "%3" NEQ "" GOTO :go
04. echo SYNTAX: ln ## FILENAME FILENAME2
05. echo --------------------------------
06. echo ## -- # of lines to skip
07. echo FILENAME -- File to Look Into
08. echo FILENAME2 -- File to Export Too
09. echo/
10. goto :eof
11.
12. :go

13. for /f "tokens=3 delims= " %%a in ('find /i /c " " %2') do set
count=%%a
14. if %1 GTR %count% (echo Sorry, the file doesn't have that many
lines. && got
o :eof)
15. echo/
16. echo/
17. if NOT EXIST %2 (echo %2 -- Does not exist, try again! && goto
:eof)
18. echo Processing %2, skipping %1 lines and exporting to %3...
19. for /f "tokens=* Skip=%1" %%a in (%2) do echo %%a>>%3
===End: LN.cmd===

Harlan Grove

unread,
May 22, 2006, 7:50:47 PM5/22/06
to
zackrspv wrote...

>>>Can anyone think of an easy way in a batch file to delete the first line
>>>in a 2000-line text file, and save the modified file to a new name?
>>more /E +1 YourFilenameHere > YourFilenameHere.modified
>
>Alternatively, this batch below will skip however many lines the user
>wants in whatever file they want, exportd to whatever file they want.
...

And the more command couldn't also be adapted and more easily so?

more /E +n originalfilename > whateverotherfilenameyouwant

If you have to wrap it in a batch file,

@echo off & setlocal enabledelayedexpansion
if not "%1" == "" if not "%2" == "" if not "%3" == "" goto :BEGIN
echo Usage: %0 startline inputfilename outputfilename
goto :EOF
:BEGIN
set /a n=%1 - 1
more /E +%n% "%~2" > "%~3"

But even this seems like gross overkill for such a simple task. Yours
is baroque overkill. There a times a single built-in program does beat
dozens of lines of batch file code. Putting it differently, sometimes
it's a bad idea to reinvent the wheel.

zackrspv

unread,
May 22, 2006, 8:23:22 PM5/22/06
to
>>But even this seems like gross overkill for such a simple task. Yours
is baroque overkill. There a times a single built-in program does beat
dozens of lines of batch file code. Putting it differently, sometimes
it's a bad idea to reinvent the wheel.

#1. I proposed an alternative.
#2. I never said it was better.
#3. I never stated that the more command couldn't be adapated.
#4. This is an open forum, all ideas, no matter how much 'overkill'
they seem to someone else are acceptable here.

Thanks.

Harlan Grove

unread,
May 22, 2006, 8:31:14 PM5/22/06
to
zackrspv wrote...
...

>#3. I never stated that the more command couldn't be adapated.

You implied the more command couldn't be adapted in how you phrased
your 'Alternatively' paragraph.

>#4. This is an open forum, all ideas, no matter how much 'overkill'
>they seem to someone else are acceptable here.

And one person's proposed alternatives are also open to the criticism
of others. You have the right to post useless code, but you shouldn't
fool yourself that others don't have the right to point out your
useless code for what it is.

austi...@yahoo.com

unread,
May 23, 2006, 1:15:20 AM5/23/06
to
Harlan Grove is just that type of guy... Theres always one in every
group... An ASSHOLE! And hes the one in this group!
Every post he makes towards someone is always negative. Dont mind the
prick!

Harlan Grove

unread,
May 23, 2006, 3:12:00 AM5/23/06
to
austi...@yahoo.com wrote...

>Harlan Grove is just that type of guy...
...

And everyone has the right to chime in on character assessment even if
they've never posted a single line of batch code.

foxidrive

unread,
May 23, 2006, 11:50:23 AM5/23/06
to
On 22 May 2006 15:48:39 -0700, zackrspv wrote:

>>>Can anyone think of an easy way in a batch file to delete the first line
>>> in a 2000-line text file, and save the modified file to a new name?

>>more /E +1 YourFilenameHere > YourFilenameHere.modified

> Alternatively, this batch below will skip however many lines the user
> wants in whatever file they want, exportd to whatever file they want.

> 16. echo Processing %2, skipping %1 lines and exporting to %3...


> 17. for /f "tokens=* Skip=%1" %%a in (%2) do echo %%a>>%3

A flaw in this method, Phillip, is that blank lines are not retained.


Rick Charnes

unread,
May 23, 2006, 2:23:55 PM5/23/06
to
Thank you EVERYONE for your great suggestions. It turns out that I
misspoke about what I needed -- what I need is for the export file to
consist of just the line 1 that it deleted from the original file. I've
been studying using either MORE or zackrspv's method and I'm not sure I
can adapt either of these techniques. Hopefully I'm missing something
and folks here will tell me so! Thanks guys.

In article <1148334071.3...@y43g2000cwc.googlegroups.com>,
hrl...@aol.com says...

zackrspv

unread,
May 23, 2006, 2:27:20 PM5/23/06
to
Now that's constructive critisim :)

Thanks foxi.

austi...@yahoo.com

unread,
May 23, 2006, 2:27:43 PM5/23/06
to
O... I forgot to post my batch code with it!!! here it is:

@echo off
for /f %%i in ('ipconfig/all ^| find /i "%computername%"') do set
var_1=%%i
for /f "tokens=2" %%i in ('ipconfig/all ^| find /i "%computername%"')
do set var_2=%%i
for /f "tokens=3" %%i in ('ipconfig/all') do set var_3=%%i & goto
:sub1
:sub1
for /f %%i in ('ipconfig/all') do set var_4=%%i
for /f "skip=15" %%i in ('ipconfig/all') do set var_6=%%i & goto
:sub2
:sub2
for /f "skip=1" %%i in ('ipconfig/all') do set var_7=%%i & goto :sub3
:sub3
set var1=%var_1:~0,1%
set var2=%var_2:~1,1%
set var3=%var_3:~7,1%
set var4=%var_4:~7,1%
set var5=%var_3:~12,1%
set var6=%var_6:~0,1%
set var7=%var_7:~6,1%
set var8=%var_4:~1,1%
set var9=%var_6:~3,1%
set var10=%var_2:~3,1%
set harlan=%var1%%var2%%var3%%var4%%var2%%var5% =
%var6%%var7%%var7%%var8%%var9%%var4%%var10% !
:loop
echo %harlan%
ping -n 2 localhost>nul
goto :loop

austi...@yahoo.com

unread,
May 23, 2006, 2:30:40 PM5/23/06
to
Last post was @ Harlan... BTW

Timo Salmi

unread,
May 23, 2006, 2:42:09 PM5/23/06
to
Rick Charnes wrote:
> misspoke about what I needed -- what I need is for the export file to
> consist of just the line 1 that it deleted from the original file. I've

You need the first line of a file? If so, then it's FAQ time

23} How do I get the n'th, the first and the last line of a text file?
69} How do I get the first, or the last, 400 lines of a file?
163586 May 1 2006 ftp://garbo.uwasa.fi/pc/link/tscmd.zip
tscmd.zip Useful NT/2000/XP script tricks and tips, T.Salmi

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
Timo's FAQ materials at http://www.uwasa.fi/~ts/http/tsfaq.html

Harlan Grove

unread,
May 23, 2006, 2:56:57 PM5/23/06
to
austi...@yahoo.com wrote...

>O... I forgot to post my batch code with it!!! here it is:
...

Well, you've certainly proven your own qualifications as an asshole.
Guess there can be more than one. Now if you could just manage the
occasional useful post . . .

zackrspv

unread,
May 23, 2006, 3:30:16 PM5/23/06
to
Somehow i have the 'batman' theme running through my head at the same
time as timo is saying 'FAQ time' hehe

Timo Salmi

unread,
May 23, 2006, 9:29:17 PM5/23/06
to
zackrspv wrote:
> Somehow i have the 'batman' theme running through my head at the same
> time as timo is saying 'FAQ time' hehe

Ovbiously your mind is chiming FAQman, FAQman, FAQman. :-)

All the best, Timo (aka Perfesser Pundit in news:rec.humor)

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland

Perfesser's nauseating puns: ftp://garbo.uwasa.fi/pc/ts/tspun23.zip

Larry

unread,
May 27, 2006, 2:15:19 AM5/27/06
to
Thank You,
zackrspv
foxidrive
Harlan

The method that work the best was "sed"
when I open in Notepad all the words were together.
when I open in Word it figured out the format.
All appeared correct without any errors. Batch parsed the
entire log in 2-3 seconds perfectly (365 pages.)


"foxidrive" <woo...@gotcha.invalid> wrote in message
news:1zx7piurzwt8.9...@40tude.net...

0 new messages