12

As the title states I need a batch file to delete the FIRST 3 lines of a text file.

for example:

A    
B    
C    
D    
E   
F    
G

in this example I need A,B and C deleted along with the line

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
kriegy
  • 195
  • 1
  • 3
  • 14
  • [See this link][1]. Hope it will work for you. [1]: http://stackoverflow.com/questions/418916/delete-certain-lines-in-a-txt-file-via-a-batch-file – Arun Jain Jul 11 '12 at 08:46
  • Arun Jain that only works if you know what is in the line i don't – kriegy Jul 11 '12 at 09:18

5 Answers5

18
more +3 "file.txt" >"file.txt.new"
move /y "file.txt.new" "file.txt" >nul

The above is fast and works great, with the following limitations:

  • TAB characters are converted into a series of spaces.
  • The number of lines to be preserved must be less than ~65535. MORE will hang, (wait for a key press), if the line number is exceeded.
  • All lines will be terminated by carriage return and linefeed, regardless how they were formatted in the source.

The following solution using FOR /F with FINDSTR is more robust, but is much slower. Unlike a simple FOR /F solution, it preserves empty lines. But like all FOR /F solutions, it is limited to a max line length of a bit less than 8191 bytes. Again, all lines will be terminated by carriage return and linefeed.

@echo off
setlocal disableDelayedExpsnsion
>"file.txt.new" (
  for /f "delims=" %%A in ('findstr /n "^" "file.txt"') do (
    set "ln=%%A"
    setlocal enableDelayedExpansion
    echo(!ln:*::=!
    endlocal
  )
)
move /y "file.txt.new" "file.txt" >nul

If you have my handy-dandy JREPL.BAT regex text processing utility, then you could use the following for a very robust and fast solution. This still will terminate all lines with carriage return and linefeed (\r\n), regardless of original format.

jrepl "^" "" /k 0 /exc 1:3 /f "test.txt" /o -

You can write \n line terminators instead of \r\n by adding the /U option.

If you must preserve the original line terminators, then you can use the following variation. This loads the entire source file into a single JScript variable, so the total file size is limited to approximately 1 or 2 gigabytes (I forgot the exact number).

jrepl "(?:.*\n){1,3}([\s\S]*)" "$1" /m /f "test.txt" /o -

Remember that JREPL is a batch file, so you must use CALL JREPL if you use the command within another batch script.

dbenham
  • 127,446
  • 28
  • 251
  • 390
11

This should do it

for /f "skip=3 delims=*" %%a in (C:\file.txt) do (
echo %%a >>C:\newfile.txt    
)
xcopy C:\newfile.txt C:\file.txt /y
del C:\newfile.txt /f /q

That will re-create the file with the first 3 lines removed.

To keep the user updated you could integrate messages in the batch file in vbscript style or output messages in the command prompt.

@echo off
echo Removing...
for /f "skip=3 delims=*" %%a in (C:\file.txt) do (
echo %%a >>C:\newfile.txt
) >nul
echo Lines removed, rebuilding file...
xcopy C:\newfile.txt C:\file.txt /y >nul
echo File rebuilt, removing temporary files
del C:\newfile.txt /f /q >nul
msg * Done!
exit >nul

Hope this helps.

Bali C
  • 30,582
  • 35
  • 123
  • 152
  • i seem to have run in to an error ">> was unexcpected at this time" – kriegy Jul 11 '12 at 09:09
  • Sorry, there was a typo, I had put a `"` sign in the filename in the `for` loop, try now. – Bali C Jul 11 '12 at 09:16
  • Sorry, I couldn't test the code when I posted it. I have edited it again and tested this time and it works. I forgot the `delims` so it was breaking when it hit a space. Try now. – Bali C Jul 11 '12 at 11:28
  • it took us a while but we got there thank you i'm going to put up another question it involves if statements ...yay – kriegy Jul 11 '12 at 11:35
  • No worries, yeah, got there in the end! :) Ok, I will look out for it! – Bali C Jul 11 '12 at 11:38
  • @kriegy - there are still problems with this script. It will truncate line at 1st `*` character. It will strip lines beginning with `;`. It will strip blank lines. There are ways to eliminate the problems, but there is a [simpler solution](http://stackoverflow.com/a/11432438/1012053) using MORE – dbenham Jul 11 '12 at 12:16
  • is it worth having vbscript msgbox GUI? – kriegy Jul 11 '12 at 12:56
  • @kriegy What do you mean? Use a messagebox for what? – Bali C Jul 11 '12 at 13:03
  • the file im writing will be used by someone who isn't as good as i am (and that is saying something) i thought would msgboxes be good so like: running, done simple stuff i know the minimum of vbscript (tiny bit) – kriegy Jul 11 '12 at 13:06
  • @kriegy Ok, see my answer, I have updated with some suggestions. – Bali C Jul 11 '12 at 13:13
  • I don't know a lot of VB, you mean like `msgbox"Hello kriegy!"` or `wscript.echo("Hello")` – Bali C Jul 11 '12 at 13:34
  • I think the batch will be fine, after all it only takes a second to do, why not just put a `echo Done` and a `pause >nul` at the end so they know it's done and they can just close it. – Bali C Jul 11 '12 at 13:45
3

Use sed to only print beginning with the 4th line (Edit: Only if you use Un*x :)

$ sed -e '4,$p' in.txt 
  • i prefer to keep it all native as i might distribute a couple of copies thank you though – kriegy Jul 11 '12 at 09:16
  • This question is tagged as [tag:batch]. You'll have to get [Sed for windows](http://gnuwin32.sourceforge.net/packages/sed.htm) to make it work. – Eitan T Jul 11 '12 at 09:54
  • If I read the answers that only use batch, I recommend to do that :) –  Jul 12 '12 at 07:06
2

I use "more" command for outting file after nth line Command (windows)

More +n orginalfilename.txt > outputfilename.txt

Description: Outputting file of txt after nth line

Sarah Filano
  • 71
  • 1
  • 1
0

If you want to skip the first and tail the last lines, you can use my code at http://www.harchut.de/download/ms-dos/skip-tail-flatfile-example.zip

set L_TRIMFILE_CNT=0
set L_TRIMFILE_TMPFILE=work.countrows.find.tmp
find /c /v "" %P_TRIMFILE_INFILE% > %L_TRIMFILE_TMPFILE%
for /f "skip=1 tokens=3* " %%a in (%L_TRIMFILE_TMPFILE%) do (
  if %L_TRIMFILE_CNT% == 0 set L_TRIMFILE_CNT=%%a
)
del %L_TRIMFILE_TMPFILE%
set /a L_TRIMFILE_EOF=%L_TRIMFILE_CNT%-%P_TRIMFILE_TAIL%
findstr /n .* "%P_TRIMFILE_INFILE%" > %L_TRIMFILE_TMPFILE%
if exist %P_TRIMFILE_OUTFILE% del %P_TRIMFILE_OUTFILE%
set V_TRIMFILE_SKIP="skip=%P_TRIMFILE_SKIP% tokens=1* delims=:"
if %P_TRIMFILE_SKIP% equ 0 set V_TRIMFILE_SKIP="tokens=1* delims=:"
for /f %V_TRIMFILE_SKIP% %%a in (%L_TRIMFILE_TMPFILE%) do (
  if %%a leq %L_TRIMFILE_EOF% if exist %P_TRIMFILE_OUTFILE% @echo.%%b>>%P_TRIMFILE_OUTFILE%
  if %%a leq %L_TRIMFILE_EOF% if not exist %P_TRIMFILE_OUTFILE% @echo.%%b>%P_TRIMFILE_OUTFILE%
)
del %L_TRIMFILE_TMPFILE%

You can skip or tail line of a flatfile without any additional utility.

Rob
  • 19
  • 2
  • 1
    You might want to add a little explanation on this answer. Always better to give a person a fishing rod than giving him a fish. – Amit May 22 '15 at 13:50