2017-02-13 116 views
0

我期待通过批处理文件删除我的.csv文件的最后11(总是11)行,并最好不创建新命名的文件。我发现下面删除前3行,但我不知道如何修改此删除最后11.任何想法?谢谢你的帮助!批处理文件,删除最后11行的CSV

@echo off 
set "csv=MyFile.csv" 
more +3 "%csv%" >"%csv%.new" 
move /y "%csv%.new" "%csv%" >nul 
+0

[Windows批处理,如何删除第一行和最后一行并保存为新的文本文件]的可能的重复(http://stackoverflow.com/questions/31444879/windows-batch-how-to-remove-first -line-and-last-line-and-save-as-a-new-text-fil) –

+0

不是重复的,我要求只删除最后11行,也不想保存为如果可能的话,新的文本/ csv文件。谢谢! – slybitz

+0

@Squashman:这是跳过前11行的计划,而不是删除***最后*** 11行。 – abelenky

回答

1

下面的代码片段替换行more +3 "%csv%" >"%csv%.new"你想要做什么,使用临时文件:

@echo off 
setlocal EnableExtensions DisableDelayedExpansion 

rem // Define constants here: 
set "_CSV_FILE=%~1" & rem // (specify CSV file here; `%~1` is first argument) 
set "_NUM_FRST=0" & rem // (specify number of lines to remove from beginning) 
set "_NUM_LAST=11" & rem // (specify number of lines to remove from end) 

rem // Count number of available lines in file: 
for /F %%C in ('^< "%_CSV_FILE%" find /C /V ""') do set /A "COUNT=%%C" 
set /A "COUNT-=_NUM_LAST" 
rem /* Process file, regarding and maintaining empty lines; 
rem lines must be shorter than about 8190 bytes: */ 
> "%_CSV_FILE%.tmp" (
    set /A "INDEX=0" 
    for /F "delims=" %%L in ('findstr /N "^" "%_CSV_FILE%"') do (
     set /A "INDEX+=1" 
     set "LINE=%%L" 
     setlocal EnableDelayedExpansion 
     if !INDEX! GTR %_NUM_FRST% if !INDEX! LEQ %COUNT% echo(!LINE:*:=! 
     endlocal 
    ) 
) 
rem // Overwriting original file: 
> nul move /Y "%_CSV_FILE%.tmp" "%_CSV_FILE%" 

endlocal 
exit /B 

下面的方法不使用临时文件:

@echo off 
setlocal EnableExtensions DisableDelayedExpansion 

rem // Define constants here: 
set "_CSV_FILE=%~1" & rem // (specify CSV file here; `%~1` is first argument) 
set "_NUM_FRST=0" & rem // (specify number of lines to remove from beginning) 
set "_NUM_LAST=11" & rem // (specify number of lines to remove from end) 

rem // Count number of available lines in file: 
for /F %%C in ('^< "%_CSV_FILE%" find /C /V ""') do set /A "COUNT=%%C" 
set /A "COUNT-=_NUM_LAST" 
rem /* Process file, regarding and maintaining empty lines; 
rem lines must be shorter than about 8190 bytes: */ 
set /A "INDEX=0" 
for /F "delims=" %%L in ('findstr /N "^" "%_CSV_FILE%" ^& ^> "%_CSV_FILE%" rem/') do (
    set /A "INDEX+=1" 
    set "LINE=%%L" 
    setlocal EnableDelayedExpansion 
    if !INDEX! GTR %_NUM_FRST% if !INDEX! LEQ %COUNT% >> "!_CSV_FILE!" echo(!LINE:*:=! 
    endlocal 
) 

endlocal 
exit /B 
+0

这仍然写入到一个新的文件,海报明确表示要避免。 – abelenky

+1

@abelenky,它写一个临时文件并最终将其移动到原始文件上;我添加了一个没有临时文件的变体... – aschipfl

0

这里是一个简单的命令来计算文件中的行。

但是,我没有看到无论如何不写入一个单独的中间文件截断文件。

@echo off 
for /F "delims=[]" %%a in ('find /N /V "" myFile.TXT') do set LINE_COUNT=%%a 
echo %LINE_COUNT% 
-3

通过head -n -11 "%csv%" > "%csv%.new"

+3

'head'不是Windows命令。 – abelenky

+0

我错过了提及我在机器上安装了Gow(Gnu On Windows)的问题。如果您有权执行此操作,请参阅以下链接: https://blogs.msdn.microsoft.com/matt-harrington/2012/06/03/from-unix-to-windows-run-gnu如果您不想部署整个可执行文件,只需复制文件C:\ Program Files(x86)\ Gow \ bin \ head.exe。 –

相关问题