Re: [SLUG] Slicing a file

From: Eben King (eben1@tampabay.rr.com)
Date: Fri May 26 2006 - 01:38:17 EDT


On Fri, 26 May 2006, Paul M Foster wrote:

> Here's what I need to do (sometimes): Take a text file and split it into
> pieces. The split command won't work for this, because the pieces may not be
> equal. For example, I may want to split an 8000 line file into pieces 2000,
> 3000 and 3000 lines each. I'm willing to run the same command multiple times
> with different parameters, but whatever I use needs to be some common command
> expected to be there on any system. I've looked at split, head and tail. I
> suspect I can do this with sed or awk, but I'm not very familiar with those
> two, beyond rudimentary use. Can anyone suggest a solution?

Find out what line is the first that should be in the second file (call it L).

head -n L-1 filename > file1
tail +L filename > file2andLater

You could also do that as

sed Lq filename > file1
sed 1,Ld filename > file2andLater

if you really like sed.

Repeat for additional divisions, leapfrogging your way through the file.

I'm sure someone will come up with a method where you get a list of split
points from the original file and use that. That method lends itself more
readily to automation.

-- 
-eben    ebQenW1@EtaRmpTabYayU.rIr.OcoPm    home.tampabay.rr.com/hactar

Hi! I'm a .sig virus! Copy me to your .sig! ----------------------------------------------------------------------- This list is provided as an unmoderated internet service by Networked Knowledge Systems (NKS). Views and opinions expressed in messages posted are those of the author and do not necessarily reflect the official policy or position of NKS or any of its employees.



This archive was generated by hypermail 2.1.3 : Fri Aug 01 2014 - 19:21:46 EDT