awk - split file

How can I split a text file (in awk) in n others with number of record given in input?
Thanks

How split? Equally or orientating on something special requirement?
A bit more precise info please :slight_smile:

Can you tell us why you have to use awk to split it, if it is not homework.

Cut is more simple in your case.

$ cat Split_file

cut -d" " -f1-$1 urfile

Run by :

Split_file 4

I would like to make the same thing of the "split" command but with a number in the end of the file name.

For exampe if I have file pippo.txt that contain 10 rows and I would like to have max 8 rows for file the result that I wolud like to have is
pippo.txt_1 with 8 rows
pippo.txt_2 with 2 rows

---------- Post updated at 05:03 AM ---------- Previous update was at 05:01 AM ----------

I have made this but I don't like so much....
Ah I use ksh

i=1
iFile=1
while read line
do
  if [ $i -gt $NUM_REC ]
  then
   iFile=`expr $iFile + 1 `
   i=1
  fi
  FILE_TO_W=${FILE_ORI}_${iFile}
  echo $line >> $FILE_TO_W
  i=`expr $i + 1 `
done <$FILE_ORI

Like this? :cool:

awk -v var=8 'BEGIN{i=1} NR > var{i++}{print > "pippo.txt_" i} ' file

Hi.

In awk:

awk -v LINES=8 'NR % LINES == 1 { FILE = FILENAME "_" ++C } { print > FILE }'  pippo.txt

I was trying to do it using csplit, but that's a complete nightmare!

Thank you very much!!!!!