Splitting file based on line numbers

Hello friends,

Is there any way to split file from n to n+6 into 1 file and (n+7) to (n+16) into other file etc.
f.e I have source pipe delimated file with 20 lines and i need to split 1-6 in file1 and 7-16 in file2 and 17-20 in file 3
I need to split into fixed number of file like 4 files based on the line numbers.

Your help is much appreciated. Thanks :slight_smile:

Something like this?

awk '
NR<=6{print > "file1"; next}
NR<=16{print > "file2"; next}
{print > "file3"}
' file

Hi.

Assume the input file is simply a sequence of integers, one per line, from 101 through 120.

If you are using explicit line numbers, then this might appear to be a natural notation:

#!/usr/bin/env bash

# @(#) s1	Demonstrate splitting a file based on line numbers.

pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C sed

FILE=${1-data1}

rm -f file file1 file2

sed -n \
-e "1,6w file" \
-e "7,16w file1" \
-e "17,20w file2" \
$FILE

pl " Sample results, file2:"
cat file2

exit 0

producing:

% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
bash GNU bash 3.2.39
sed GNU sed version 4.1.5

-----
 Sample results, file2:
117
118
119
120

See man sed for detials.

Best wishes ... cheers, drl