Splitting files from one file

Hi,

I have an input file like:

111
abcdefgh
asdfghjk
dfghjkl
222
aaaaaaa
bbbbbb
333
djfhfgjktitjhgfkg
444
djdhfjkhfjkghjkfg
hsbfjksdbhjkgherjklg
fjkhfjklsahjgh
fkrjkgnj

I want to read this input file and make separate output files with the header as numric value like "111" and the rest of alphabetic data following it. similarly, i want to have 3 other files with numeric header (like 222, 333, 444) and rest of data following them.

I have created the following script but its not helping..

#!/usr/bin/ksh
set -x
linecount=`cat testfile|wc -l`
numval=`cat testfile|grep ^[0-9]|wc -l`
i=0
set -A arr `awk '/[0-9]/{print NR}' testfile`
while [ $linecount -gt 0 ]
do
   while [ $i -le $numval ]
   do
     val=`echo ${arr}`
     cat testfile|head -${val} > file${i}
     i=`expr $i + 1`
   done
     linecount=`expr $linecount - 1`
done

Try this:

awk '/[0-9]/{of=$0}{print > of}' inputfile

I tried running the command separately. where will the 4 o/p files be created??

You don't have to use the script, the oneliner is sufficient. The files should be in the current directory.

Regards

Thanks Frank..This is working absolutely fine but can you please be kind enuf to explain me this "awk" statement

Here goes the explanation

awk  '...' abc.txt

for all values in abc.txt

/[0-9]/{of=$0}

if the line matches 0,1,2...9 then the variable of is set to the value (ie the file name is set the numeric value)

{print > of}

print the line to the file name set in the variable 'of'

HTH,
PL

But how does awk makes sure that it has to split till the next number occurence...like how to know what is delimiter...

Hi saltysumi,

Below is the code if you need it as a shell script

#!/bin/ksh
 _prevseq=1
grep -n '^[0-9]' input.txt|cut -f1 -d":"|tail +2|while read _txt
do
 echo ${_txt}
   _curval=`expr ${_txt} - ${_prevseq}`
   tail +${_prevseq} input.txt|head -${_curval} > ${_prevseq}.txt
   _prevseq=${_txt}
done
   tail +${_prevseq} input.txt|head -${_curval} > ${_prevseq}.txt

Awk try to find the line having number , if it finds assign it to "of" , so all the lines till the next number will stored in a file created as value of "of". Make sure the first line in the input file is a number , otherwise it will create a file name as "of" and stores all the lines till the occurrence of a number.

while(<DATA>){
  if(/^([0-9]*)$/){
  	my $file=$1.".txt";
  	close FH if FH;
  	open FH,">$file";
  	print FH $_;
  	next;
  }
  else{
  	print FH $_;
  }
}
__DATA__
111
abcdefgh
asdfghjk
dfghjkl
222
aaaaaaa
bbbbbb
333
djfhfgjktitjhgfkg
444
djdhfjkhfjkghjkfg
hsbfjksdbhjkgherjklg
fjkhfjklsahjgh
fkrjkgnj