如何删除.csv文件中的第1行和第3至19行? [英] How to delete lines 1, and 3 to 19 in a .csv file?

查看:93
本文介绍了如何删除.csv文件中的第1行和第3至19行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想删除csv文件的第1行和第3至19行.我能够删除所有19行,但无法保留我需要的行(第二行)通过使用其他帖子,我的脚本如下所示

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 

所以我删除第一行(然后我想要的行现在在顶部),我想从第二行开始删除17行.我不知道如何跳过第一行.

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.

推荐答案

如果您的文件只有19行,那么您只想保留第2行.最简单的解决方案是翻转逻辑并担心要添加的行保持.

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.

该字符的最大行数限制为1021,并且尾随的控制字符将被删除.

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%"

使用FOR/F支持的行长最多为8191.

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

EDIT

现在,我知道源文件有19行以上,比我使用 Stephan的答案要多,除非其中之一以下限制成为问题:

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一行最多只能读取1021个字符
  • 更多会将制表符转换为一串空格
  • 如果文件足够大(我认为超过64K行),那么即使重定向了MORE的输出,输出也会最终挂起,等待按键.

如果上述任何限制是一个问题,并且所有行的长度均少于8191个字符,并且没有任何行以:开头,则可以进行以下操作.但这比较慢.

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

但是,如果您想要一个没有任何限制的真正快速的解决方案,则可以使用我的 REPL.BAT实用程序-a混合JScript/批处理脚本,在stdin上执行正则表达式搜索/替换操作,并将结果写入stdout.它是纯脚本,可​​以从XP开始在任何现代Windows机器上本地运行.完整的文档嵌入在脚本中.

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

最初的FINDSTR只是在每行前面加上行号,后跟冒号.

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

如果REPL是1或3-19,则第一个REPL会删除行号.

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

最后一个REPL删除以数字开头,后跟冒号的所有前缀,并丢弃未修改的行.

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

这篇关于如何删除.csv文件中的第1行和第3至19行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆