使用 Windows 批处理脚本将文本文件拆分为多个文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20807806/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 11:12:37  来源:igfitidea点击:

Split text file into multiple files using windows batch scripting

windowsfilebatch-filesplit

提问by user3140605

I need to split one text file into multiple files using windows batch script, could anybody light me up?

我需要使用 Windows 批处理脚本将一个文本文件拆分为多个文件,有人可以点亮我吗?

sample text file:

示例文本文件:

abc1-10
abc1-11
abc1-12
xyz2-01
xyz2-02
xyz3-01
xyz3-02

in this case, it has to split into 3 files, first one consists the lines abc1-xx, second one consists xyz2-xxand xyz3-xxgo to the last one

在这种情况下,它必须分成 3 个文件,第一个包含行abc1-xx,第二个包含xyz2-xxxyz3-xx转到最后一个

回答by fthiella

You could use a batch file, but why not just use FINDSTRcommand?

您可以使用批处理文件,但为什么不直接使用FINDSTR命令呢?

findstr /R "^abc1-" sample.txt > file1.txt
findstr /R "^xyz2-" sample.txt > file2.txt
findstr /R "^xyz3-" sample.txt > file3.txt

回答by hhh

Use the cgwin command SPLIT.

使用 cgwin 命令 SPLIT。

Samples:

样品:

-split a file every 500 lines counts:

- 每 500 行拆分一个文件计数:

      split -l 500 [filename.ext]

For more: split --help

更多信息:split --help

回答by foxidrive

This may help - it will split the text into separate files of

这可能会有所帮助 - 它会将文本拆分为单独的文件

abc1.txt
xyz2.txt
xyz3.txt

abc1.txt
xyz2.txt
xyz3.txt

@echo off
for /f "tokens=1,* delims=-" %%a in ('type "file.txt"') do (
>>"%%a.txt" echo(%%a-%%b
)
pause