问题
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.
回答1:
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"
)
)
回答2:
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
回答3:
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
回答4:
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)
回答5:
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
-ioptions force the replacement directly in input file1drequests removing first line$drequests removing last line
You can download GOW from Gow GITHUB
来源:https://stackoverflow.com/questions/31444879/windows-batch-how-to-remove-first-line-and-last-line-and-save-as-a-new-text-fil