使用 Windows 命令行连接文本文件,删除前导行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2477271/
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
Concatenate text files with Windows command line, dropping leading lines
提问by James
I need to concatenate some relatively large text files, and would prefer to do this via the command line. Unfortunately I only have Windows, and cannot install new software.
我需要连接一些相对较大的文本文件,并且更愿意通过命令行执行此操作。不幸的是,我只有 Windows,无法安装新软件。
type file1.txt file2.txt > out.txt
allows me to almost get what I want, but I don't want the 1st line of file2.txt to be included in out.txt.
允许我几乎得到我想要的东西,但我不希望 file2.txt 的第一行包含在 out.txt 中。
I have noticed that more
has the +n
option to specify a starting line, but I haven't managed to combine these to get the result I want. I'm aware that this may not be possible in Windows, and I can always edit out.txt by hand to get rid of the line, but is there a simple way of doing it from the command line?
我注意到,more
有+n
指定的起始行选项,但我还没有成功地组合这些得到我想要的结果。我知道这在 Windows 中可能是不可能的,我总是可以手动编辑 out.txt 以摆脱该行,但是有没有一种从命令行执行此操作的简单方法?
回答by ghostdog74
more +2 file2.txt > temp
type temp file1.txt > out.txt
or you can use copy
. See copy /?
for more.
或者你可以使用copy
. 查看copy /?
更多。
copy /b temp+file1.txt out.txt
回答by Raj More
I use this, and it works well for me:
我使用这个,它对我很有效:
TYPE \\Server\Share\Folder\*.csv >> C:\Folder\ConcatenatedFile.csv
TYPE \\Server\Share\Folder\*.csv >> C:\Folder\ConcatenatedFile.csv
Of course, before every run, you have to DELETE C:\Folder\ConcatenatedFile.csv
当然,在每次跑步之前,你必须 DELETE C:\Folder\ConcatenatedFile.csv
The only issue is that if all files have headers, then it will be repeated in all files.
唯一的问题是,如果所有文件都有标题,那么它将在所有文件中重复。
回答by John Faughnan
I don't have enough reputation points to comment on the recommendation to use *.csv >> ConcatenatedFile.csv
, but I can add a warning:
我没有足够的声望点数来评论使用的建议*.csv >> ConcatenatedFile.csv
,但我可以添加一个警告:
If you create ConcatenatedFile.csv
file in the same directory that you are using for concatenation it will be added to itself.
如果您ConcatenatedFile.csv
在用于连接的同一目录中创建文件,它将被添加到自身中。
回答by Alberto Rossini
Use the FOR command to echo a file line by line, and with the 'skip' option to miss a number of starting lines...
使用 FOR 命令逐行回显文件,并使用 'skip' 选项错过许多起始行...
FOR /F "skip=1" %i in (file2.txt) do @echo %i
You could redirect the output of a batch file, containing something like...
您可以重定向批处理文件的输出,其中包含类似...
FOR /F %%i in (file1.txt) do @echo %%i
FOR /F "skip=1" %%i in (file2.txt) do @echo %%i
Note the double % when a FOR variable is used within a batch file.
请注意在批处理文件中使用 FOR 变量时的双 %。
回答by Waldo
Here's how to do this:
以下是如何执行此操作:
(type file1.txt && more +1 file2.txt) > out.txt
回答by Brian D
I would put this in a comment to ghostdog74, except my rep is too low, so here goes.
more +2 file2.txt > temp
This code will actually ignore rows 1 and 2 of the file. OP wants to keep all rows from the first file (to maintain the header row), and then exclude the first row (presumably the same header row) on the second file, so to exclude only the header row OP should use more +1
.
我会把这个放在对 ghostdog74 的评论中,除非我的代表太低,所以就这样吧。
more +2 file2.txt > temp
此代码实际上将忽略文件的第 1 行和第 2 行。OP 想要保留第一个文件中的所有行(以维护标题行),然后在第二个文件中排除第一行(大概是相同的标题行),因此仅排除标题行 OP 应使用more +1
.
type temp file1.txt > out.txt
It is unclear what order results from this code. Is temp
appended to file1.txt
(as desired), or is file1.txt
appended to temp
(undesired as the header row would be buried in the middle of the resulting file).
In addition, these operations take a REALLY LONG TIME with large files (e.g. 300MB)
type temp file1.txt > out.txt
目前尚不清楚此代码产生的顺序。被temp
附加到file1.txt
(如需要的话),或者被file1.txt
附加到temp
(不期望作为标题行会被埋没在产生的文件的中间)。
此外,对于大文件(例如 300MB),这些操作需要很长时间
回答by Andrew Mao
I know you said that you couldn't install any software, but I'm not sure how tight that restriction is. Anyway, I had the same issue (trying to concatenate two files with presumably the same headers) and I thought I'd provide an alternative answer for others who arrive at this page, since it worked just great for me.
我知道你说过你不能安装任何软件,但我不确定这个限制有多严格。无论如何,我遇到了同样的问题(试图连接两个文件可能具有相同的标题),我想我会为到达此页面的其他人提供替代答案,因为它对我来说非常有用。
After trying a whole bunch of commands in windows and being severely frustrated, and also trying all sorts of graphical editors that promised to be able to open large files, but then couldn't, I finally got back to my Linux roots and opened my Cygwin prompt. Two commands:
在 Windows 中尝试了一大堆命令并感到非常沮丧,并尝试了各种承诺能够打开大文件但后来不能的图形编辑器之后,我终于回到了我的 Linux 根并打开了我的 Cygwin迅速的。两条命令:
cp file1.csv out.csv
tail -n+2 file2.csv >> out.csv
For file1.csv
800MB and file2.csv
400MB, those two commands took under 5 seconds on my machine. In a Cygwin prompt, no less. I thought Linux commands were supposed to be slow in Cygwin but that approach took far less effort and was way easier than any windows approach I could find.
对于file1.csv
800MB 和file2.csv
400MB,这两个命令在我的机器上花费的时间不到 5 秒。在 Cygwin 提示符下,同样如此。我认为 Linux 命令在 Cygwin 中应该很慢,但这种方法花费的精力要少得多,而且比我能找到的任何 Windows 方法都要容易得多。
回答by JaR
In powershell:
在 PowerShell 中:
Get-Content file1.txt | Out-File out.txt
Get-Content file2.txt | Select-Object -Skip 1 | Out-File -Append out.txt
回答by Hamed
You can also simply try this
你也可以简单地试试这个
type file2.txt >> file1.txt
It will append the content of file2.txt at the end of file1.txt
它将在 file1.txt 的末尾附加 file2.txt 的内容
If you need original file1.txt, take a backup beforehand. Or you can do this
如果需要原始file1.txt,请事先备份。或者你可以这样做
type file1.txt > out.txt
type file2.txt >> out.txt
If you want to have a line break at the end of the first file, you can try the following command before appending.
如果你想在第一个文件的末尾有一个换行符,你可以在追加之前尝试以下命令。
type file1.txt > out.txt
printf "\n" >> out.txt
type file2.txt >> out.txt
回答by GaTechThomas
The help for copy
explains that wildcards can be used to concatenate multiple files into one.
帮助copy
解释了通配符可用于将多个文件连接成一个。
For example, to copy all .txt files in the current folder that start with "abc" into a single file named xyz.txt:
例如,要将当前文件夹中以“abc”开头的所有 .txt 文件复制到名为 xyz.txt 的单个文件中:
copy abc*.txt xyz.txt