How to delete lines 1, and 3 to 19 in a .csv file?

倾然丶 夕夏残阳落幕 提交于 2021-02-10 10:09:11

问题


I'd like to delete lines 1 and 3 to 19 of a csv file. I am able to delete all the 19 lines but not able to preserve the line I need (2nd line) By using other posts, my script looks like the following

@echo off                                                                                          
set "csv=test.csv"  
more +1 "%csv%" >"%csv%.new"   
move /y "%csv%.new" "%csv%" >nul  
----missing instruction ---  
more +17  "%csv%" >"%csv%.new"  
move /y "%csv%.new" "%csv%" >nul 

So I delete the first line (then my desired line is now at the top), and I'd like to delete 17 lines starting in the 2nd one. I do not know how to skip the first line.


回答1:


If your file only has 19 lines, then you only want to keep line 2. The easiest solution is to flip the logic and worry about the line you want to keep.

Here are a few options for extracting the 2nd line.

This one is limited to a maximum line length of 1021, and trailing control characters will be stripped.

@echo off
setlocal enableDelayedExpansion
set "csv=test.csv"
<"%csv%" (
  set /p "ln="
  set /p "ln="
)
(echo(!ln!)>"%csv%"

Use of a FOR /F supports line lengths up to nearly 8191.

@echo off
setlocal
set "csv=test.csv"
for /f "usebackq skip=1 delims=" %%A in ("%csv%") do (
  (echo(%%A)>"%csv%"
  goto :break
)
:break

If you know that your target line does not begin with :, then

@echo off
setlocal
set "csv=test.csv"
for /f "tokens=1* delims=:" %%A in (
  'findstr /n "^" "%csv%" ^| findstr /b "2:"'
) do (echo(%%B)>"%csv%"

EDIT

Now that I know the source file has more than 19 lines, than I would use Stephan's answer, unless one of the following limitations becomes a problem:

  • SET /P cannot read more than 1021 characters in a line
  • MORE will convert tabs into a string of spaces
  • If the file is large enough (I think more than 64K lines) then the output will eventually hang, waiting for a key press, even though the MORE output is redirected.

IF any of the above limitations are a problem, and all lines are less than 8191 characters long, and no line begins with :, then the following will work. But it is slower.

@echo off
setlocal
set "csv=test.csv"
>"%csv%.new" (
  for /f "tokens=1* delims=:" %%A in ('findstr /n "^" "%csv%"') do (
    if %%A equ 2  echo(%%B
    if %%A gtr 19 echo(%%B
  )
)
move /y "%csv%.new" "%csv%" >nul

But if you want a really fast solution without any restrictions, then you could use my REPL.BAT utility - a hybrid JScript/batch script that performs a regex search/replace operation on stdin and writes the result to stdout. It is pure script that will run natively on any modern Windows machine from XP onward. Full documentation is embedded within the script.

@echo off
setlocal
set "csv=test.csv"
findstr /n "^" "%csv%"|repl "^(1|10|11|12|1?[3456789]):" ":"|repl "^\d+:" "" A >"%csv%.new"
move /y "%csv%.new" "%csv%" >nul

The initial FINDSTR simply prefixes each line with the line number, followed by a colon.

The first REPL strips the line number before the colon if it is 1 or 3-19

The last REPL removes any prefix beginning with a number followed by a colon, and discards lines that are not modified.




回答2:


(
set /p unused=
set /p line2=
)<%csv%
echo %line2%>%csv%.new
more +19 %csv% >>%csv%.new
rem move /y "%csv%.new" "%csv%" >nul

remove the rem when it looks ok



来源:https://stackoverflow.com/questions/22833867/how-to-delete-lines-1-and-3-to-19-in-a-csv-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!