Breaking large file into pieces

Tim.Ouellette Tim.Ouellette at FtCollins.NCR.COM
Wed Sep 12 18:08:10 AEST 1990


>>>>> On 11 Sep 90 13:42:38 GMT, monroe at dg-rtp.dg.com (Mark A Monroe) said:

Mark> I want to rip a large file into pieces, naming new files according
Mark> to an ID string in the large file.  For example, the large file contains
Mark> records that look like this:

Mark> xxx-00001239	data	data	data
Mark> description
Mark>        .
Mark>        .
Mark> (variable length)
Mark>        .
Mark> 						<---blank line
Mark> xxx-00001489	data	data	data
Mark> description
Mark>        .
Mark>        .
Mark> (variable length)
Mark>        .
Mark> 						<---blank line
Mark> xxx-00001326	data	data	data

Mark> When I find a line in the large data file that starts
Mark> with "xxx-0000", I want to open a file named "xxx-0000<number>",
Mark> like "xxx-00001489", and write every line, including
Mark> the current one, into it.  When I see another "xxx-0000",
Mark> I want to close the file, open a new file named for the new id 
Mark> string, and continue writing.  At the end of the large data
Mark> file, close all files and exit.


Mark,
   Here's an awk solution.
------------------------split.awk-----------------------

BEGIN{pcFile="/dev/null";}
/xxx-[0123456789]+/{close(pcFile);pcFile = $1;}
{print $0 >> pcFile;}

--------------------------------------------------------

execute it by
awk -f split.awk datafile

Hope this helps
--
Timothy R. Ouellette
NCR Microelectronics                   Tim.Ouellette at FtCollins.ncr.com
Ft. Collins, CO.        	       uunet!ncrlnk!ncr-mpd!bach!timo
             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"If all the world is a stage, I want to run the trap door" -- P. Beaty



More information about the Comp.unix.shell mailing list