1

text1.txt

20150716
aaaa
bbbb
cccc
dddd
END

I would like to remove 20150716, then the whole text will move up by 1 line and remove the last particular string "END". Finally, save as a new file called text1_copy.txt. Then, file becomes:

aaaa
bbbb
cccc
dddd

Any ideas? Thanks.

KenyKeny
  • 121
  • 1
  • 3
  • 19
  • 2
    Combine http://stackoverflow.com/questions/11428692/batch-file-to-delete-first-3-lines-of-a-text-file and http://stackoverflow.com/questions/12345964/deleting-last-n-lines-from-file-using-batch-file to get an answer – zedfoxus Jul 16 '15 at 03:33
  • Thank zedfoxus to post a reference links, i found useful with the first link. `for /f "skip=1 delims=*" %%a in (file.txt) do (echo %%a >> newfile.txt )` It really remove the first line and remaining lines move up by one. __But__, for the 2nd link ,i just did't get it ,it may be a little bit complex to me. Any way, i found a solution on how to remove "END" which is using loop and keeping read-in and output to a newfile2.txt line by line until the loop hit "END", i will post my solution later. – KenyKeny Jul 17 '15 at 02:50

5 Answers5

5

This just skips the first line and does not write the last one.

@echo off
    setlocal enableextensions disabledelayedexpansion

    for %%a in ("text1.txt") do > "%%~dpna_copy%%~xa" (
        set "line="
        for /f "skip=1 tokens=* delims=0123456789" %%a in ('
            findstr /n /r "^" "%%a"
        ') do (
            if defined line (
                setlocal enabledelayedexpansion
                echo(!line:~1!
                endlocal
            )
            set "line=%%a"
        )
    )
MC ND
  • 69,615
  • 8
  • 84
  • 126
3
more +1 t.txt|findstr /vX "END" > text1_copy.txt

where more +1 skips the first line
and findstr /vx "END" deletes every line, that is exactly END (lines like ENDING or THE END will not be removed)

Stephan
  • 53,940
  • 10
  • 58
  • 91
1

check the tailhead.bat which will allow you to display lines from file by numbers:

call tailhead.bat -file="text1.txt" -begin=2 -end=-2 >newfile.txt
npocmaka
  • 55,367
  • 18
  • 148
  • 187
1

I made a simple .bat file for this called cut.bat

set file=%~1
set outfile=%~2
if exist %file% goto Main
:error
echo Invalid File!
goto end
:main
for /F "delims=" %%j in ('more +1 %file%') do (
  if  defined row echo.!row!>> %outfile%
  set row=%%j
)
:end

all you do is

call cut.bat test1.txt output.txt

Vadim
  • 11
  • 1
0

For your case, for the fun, you can to that in 3 pure and simpe DOS command

findstr /V "20150716" input.txt > output.1.txt
findstr /V "END" output.1.txt > output.2.txt
copy output.2.txt input.txt

For information, using "(20150716|END)" Reges with FINDSTR DOS command seems not working with /V option !

But using SED command after having installed GOW or CYGWIN is the top

sed -i "1d;$d" input.txt

where

  • -i options force the replacement directly in input file
  • 1d requests removing first line
  • $d requests removing last line

You can download GOW from Gow GITHUB

schlebe
  • 3,387
  • 5
  • 37
  • 50