批量删除在文本文件中的行? [英] Batch Delete a line in a text file?

查看:1108
本文介绍了批量删除在文本文件中的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我拉我的头发找到一个DOS批处理文件,将删除几千txt文件的第一行,并保存该文件的原文件名的一个简单的例子。以下由另一程序进行的间歇工艺,然后我不得不ADD删除线(文本字符串组成的X,Y,Z)的每个文件的开头以下外部处理

I'm pulling my hair out finding a simple example of a DOS batch file that will delete the first line of several thousand txt files and save the file with the original file name. Following a batch process performed by another program, I then have to ADD the deleted line ( a text string consisting of "X,Y,Z") at the beginning of each file following the external processing.

推荐答案

您可以使用更多+1 来跳过该文件的第一行。然后,您可以通过管道将它变成一个临时(您不能编辑到位文本文件):

You can use more +1 to skip the first line of the file. Then you can pipe it into a temporary one (you cannot edit text files in place):

for %x in (*.txt) do (more +1 "%x">tmp & move /y tmp "%x")

之后,你可以使用类似的技术重新添加的第一行:

After that you can use a similar technique to re-add the first line:

for %x in (*.txt) do ((echo X,Y,Z& type "%x")>tmp & move /y tmp "%x")

如果您使用的批处理文件,记得要加倍迹象:

If you use those in a batch file, remember to double the % signs:

@echo off
for %%x in (*.txt) do (
    more +1 "%%x" >tmp
    move /y tmp "%%x"
)
rem Run your utility here
for %%x in (*.txt) do (
    echo X,Y,Z>tmp
    type "%%x" >>tmp
    move /y tmp "%%x"
)

好吧,显然更多不会过大的文件,这令我感到奇怪的工作。作为替代方案,如果你的文件不包含空行(虽然它看起来像CSV从我收集的),它应该工作:

Ok, apparently more doesn't work with too large files, which surprises me. As an alternative, which should work if your file does not contain blank lines (though it looks like CSV from what I gathered):

for %%x in (*.txt) do (
    for /f "skip=1 delims=" %%l in ("%%x") do (>>tmp echo.%%l)
    move /y tmp "%%x"
)

这篇关于批量删除在文本文件中的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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