批量删除文本文件中的一行?
我正在绞尽脑汁地寻找一个简单的 DOS 批处理文件示例,它将删除数千个 txt 文件的第一行,并以原始文件名保存该文件。在另一个程序执行批处理之后,我必须在外部处理之后的每个文件的开头添加已删除的行(由“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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
more +1
跳过文件的第一行。然后您可以将其通过管道传输到临时文件中(您无法就地编辑文本文件):之后您可以使用类似的技术重新添加第一行:
如果您在批处理文件中使用这些内容,请记住将
%
标志:好的,显然
more
不适用于太大的文件,这让我感到惊讶。作为替代方案,如果您的文件不包含空行(尽管它看起来像我收集的 CSV),则应该可以使用: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):After that you can use a similar technique to re-add the first line:
If you use those in a batch file, remember to double the
%
signs: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):这是我的版本。
Here's my version.