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

Re: [OT] splitting files based on keyword



On 2005-06-22, 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
>
> date 1-2-2005
> line3
> line4
>
> 1-3-2005.txt will be
> line5
> line6
> line7
> line8
>
>
> There is absolutely no correlation between the date and the number of 
> lines that it might contain. I just chose 3 dates, But in my file there 
> are about 1000 such sections and I do not want to do it manually. I am 
> wondering if there is any tool out there which does the above job.
>
> I can do it with c or fortran but that does not appear to be elegant.
>
> I looked at split, csplit. But they dont seem to do this. Googling did 
> not help much. Any other ideas?

awk '/^date/ { if (filename != "") close(filename)
               filename = $2 ".txt" }
             {print > filename}' humongous.txt

-- 
    Chris F.A. Johnson                     <http://cfaj.freeshell.org>
    ==================================================================
    Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
    <http://www.torfree.net/~chris/books/cfaj/ssr.html>



Reply to: