[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: [OT] splitting files based on keyword



Kevin Mark wrote:

On Tue, Jun 21, 2005 at 09:57:07PM -0400, kamaraju kusumanchi wrote:
I have a large text file (humongous.txt) with the following structure


date 1-1-2005
line1
line2
date 1-2-2005
line3
line4
date 1-3-2005
line5
line6
line7
line8
.....

Is there any tool which will split the above file and give me three smaller files 1-1-2005.txt, 1-2-2005.txt, 1-3-2005.txt etc., where

1-1-2005.txt will be

date 1-1-2005
line1
line2

1-2-2005.txt will be
Hi raj,
perl would be faster than bash, but here is a bash script.
-------------------------------------------
SOURCE=$1
F=""
cat $1 | while read line; do
       TEST=$(echo $line | grep ".*-.*-.*")
       if [ "$TEST" ]; then
               echo $line
               F=$(echo $line|awk '{print $2.".txt"}')
       else
               if [ -e "$F" ]; then
                       echo "$line" >> $F
               else
                       echo "$line" > $F
               fi
       fi
done
--------------------------------------------------------
Cheers,
kev

Thanks for the code Kevin. It does the job. Thanks for other ideas too. I will try to learn a scripting language soon...

raju



Reply to: