5

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

0

3 Answers 3

6

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
Sign up to request clarification or add additional context in comments.

3 Comments

This is correct in an academic sense, but when the prefix is unknown and variable then it becomes unworkable.
And what happens if the data line contains the target string other than at the beginning?
thanks fthiella, I already came up with the solution after posting this message
6

Use the cgwin command SPLIT.

Samples:

-split a file every 500 lines counts:

      split -l 500 [filename.ext]

For more: split --help

Comments

5

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

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

1 Comment

thanks foxidrive, I already came up with the solution after posting this message

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.