Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split text file into multiple files using windows batch scripting

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

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-xx and xyz3-xx go to the last one

like image 976
user3140605 Avatar asked Dec 27 '13 21:12

user3140605


People also ask

What is %% A in batch script?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. ( <set> ) Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.


2 Answers

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

findstr /R "^abc1-" sample.txt > file1.txt
findstr /R "^xyz2-" sample.txt > file2.txt
findstr /R "^xyz3-" sample.txt > file3.txt
like image 111
fthiella Avatar answered Sep 20 '22 19:09

fthiella


Use the cgwin command SPLIT.

Samples:

-split a file every 500 lines counts:

      split -l 500 [filename.ext]

For more: split --help

like image 27
hhh Avatar answered Sep 20 '22 19:09

hhh