Splitting a file by arbitrary values

How do I split a file into unequal line number segments? For example, I have a 100,000 line file that I need to split into 4 or 5 segments and the segments are not uniform, so split will not work in this case. The five segments might be: 27,000 44,512 13,542 2,344 & 12,602.

I can accomplish this by copying sections of files doing:

sed -n '1,27000p' filename
sed -n '27001,71513p' filename
sed -n '71514,85055p' filename
etc

I am wondering what the more elegant method(s) would be

Thanks

You can do something like this, the created files are file1, file2 etc.

awk '
NR==1{f="file1"}
NR==27001{f="file2"}
NR==71514{f="file3"}
{print > f}
' file

Yes.

Just to add that there is also csplit. While almost completely incomprehensible :-), it is more flexible than split.

Scottn - can you demonstrate how csplit would work for the example given?

Franklin52 - That is a valid awk alternative, but it is not much more elegant than the sed method.

Thanks for the ideas though.

I suppose the simplest form would be

csplit -f filename. filename 27001 71514 85056

If 85056 goes past the end of your file, leave it off.

Check the man page.