-2

Good day to all,

I was wondering how to split a file based on the quantity of number lines, this number passed as variable and each part has the same size (same line numbers), and each part save it into a file.

This is what I have tried:

awk -v var="$1" ' FNR==var {close("Partfile"f);f++}{print $0 > "Partfile"f}' datafile

Thanks in advance for any clue

Another.Chemist
  • 2,386
  • 3
  • 29
  • 43
  • possible duplicate of [Unix: How to split a file into equal parts, without breaking individual lines?](http://stackoverflow.com/questions/7764755/unix-how-to-split-a-file-into-equal-parts-without-breaking-individual-lines) – tripleee Aug 06 '14 at 17:44
  • mmm... nope, I don't care about the integrity of the line and I request on awk, not on split – Another.Chemist Aug 06 '14 at 17:46
  • Post some sample input and expected output as your question is open to interpretation in several different ways. And make the input the difficult cases to handle, not the easy ones. – Ed Morton Aug 06 '14 at 17:54

2 Answers2

5

Use split

split -l 20 file Partfile

to split in lumps of 20 lines each.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
1

With awk:

awk -v var="$1" '(FNR % var) == 1 { ++n; close(f); f = "Partfile" n } { print > f }' datafile
konsolebox
  • 72,135
  • 12
  • 99
  • 105