I have a text file containing a list of strings. The strings are separated by newlines and have the same length, 8-digit. I need to split larger file into smaller chunks, where each chunk contains 4 strings, all strings in the same sequence as they are in a large file.

So I need to create 16 files, 15 files x 4 string each + 1 file x 2 strings. The files should be named as list1.txt, list2.txt, etc.

What is simplest way to solve this using tools such as awk, sed, etc.?

  • So many person are downvoting because it's so easy that no one thinks you tried to search it on duck duck go (or whatever) before asking. Please read again the first point of How to Ask a good question. – andcoz Jul 27 '18 at 9:40

You can easily use split.

split --lines=4 --additional-suffix=".txt" --numeric-suffixes inputfile list 

where inputfile is, obviously, the input file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.