windows 删除文本文件的第一行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/600653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Delete first line of text file
提问by Guido Domenici
I need a cmd script that deletes the first line in my text file. The scenario is the following: I take a txt file from FTP everyday, the problem is that it comes with blank line at the top then the headers of the file. Since I'm importing that file automatically into an access table, that blank line is causing me problems.
我需要一个 cmd 脚本来删除我的文本文件中的第一行。场景如下:我每天从 FTP 中获取一个 txt 文件,问题是它的顶部带有空行,然后是文件的标题。由于我将该文件自动导入到访问表中,因此该空行给我带来了问题。
So, I need a script that deletes the blank line and saves the file.
所以,我需要一个删除空行并保存文件的脚本。
回答by Guido Domenici
Windows/command prompt:
Windows/命令提示符:
more +1 filename.ext > otherfilename.ext
That seems to work fine, however it appears that this also converts tab characters into multiple spaces.. I needed to remove the first line of a tab-delimited file before importing into postgres. That failed due to the automatic conversion of tabs to spaces by more...
这似乎工作正常,但似乎这也将制表符转换为多个空格。我需要在导入 postgres 之前删除制表符分隔文件的第一行。由于更多的制表符自动转换为空格而失败了...
回答by Adam Rosenfield
You didn't specify a platform. Here's how to do it in any *NIX environment (and Windows+Cygwin):
您没有指定平台。以下是在任何 *NIX 环境(和 Windows+Cygwin)中执行此操作的方法:
sed -i~ 1d target-file
回答by Renze de Waal
To remove the first line, I would use
要删除第一行,我会使用
tail -n +2 source-file > target-file
If you want this on Windows, download the gnu utils to obtain a tail command. +2 means "Start at the second line".
如果您想在 Windows 上使用它,请下载 gnu utils 以获取 tail 命令。+2 表示“从第二行开始”。
回答by Preet Sangha
In windows without extra tools:
在没有额外工具的窗口中:
findstr /V /R "^$" filename.whatever
No extra tools needed
不需要额外的工具
回答by Nick Painter
I noticed some comments asking how to use Preet Sangha's solution. As I do not have enough rep to add a comment, I wanted to post a more complete solution here.
我注意到一些评论询问如何使用 Preet Sangha 的解决方案。由于我没有足够的代表来添加评论,我想在这里发布一个更完整的解决方案。
You can use Preet Sangha's solution as follows. This script will create a new text file without the first line of the input file.
您可以使用 Preet Sangha 的解决方案如下。此脚本将创建一个没有输入文件第一行的新文本文件。
findstr /V /R "^$" InputFile.txt > OutputFileNameWithFirstLineRemoved.txt
回答by Jimadine
I needed to do something similar today and this was what I came up with:
我今天需要做一些类似的事情,这就是我想出的:
FOR /F "tokens=* skip=1" %A IN ('type "input_file.ext"') DO @echo %A>>"output_file.ext"
This has the advantage over the more +1
solution in that tab characters will be preserved. However, on large files this method is a lot slower than others. Also - leading spaces will be left-trimmed, which may or may not be a problem.
这比more +1
解决方案的优势在于将保留制表符。但是,在大文件上,此方法比其他方法慢得多。此外 - 前导空格将被左修剪,这可能是也可能不是问题。