Re: [SLUG] Slicing a file

From: Eben King (eben1@tampabay.rr.com)
Date: Fri May 26 2006 - 11:29:40 EDT


On Fri, 26 May 2006, Daniel Jarboe wrote:

>> A (real) example of the above would be (assuming original file contains
>> 10000 lines, and needs splits of 2000, 3000, 3000 and 2000):
>>
>>
>> head -n 2000 original > result1
>> tail -n 8000 original > temp1
>> head -n 3000 temp1 > result2
>> tail -n 5000 temp1 > temp2
>> head -n 3000 temp2 > result3
>> tail -n 2000 temp2 > result4
>>
>> The above will work, but leaves temp files potentially all over the
>> place (temp1 and temp2). If necessary, I can do that. But it would be
>> better to create the exact "chunks" from a file without any extra files
>> lying around.
>
> If you wanted to avoid files you could do some head/tail pipery, but
> sed can accomplish the same thing:
>
> sed '1,2000!d' original > result1
> sed '2001,5000!d' original > result2
> sed '5001,8000!d' original > result3
> sed '8001,10000!d' original > result4

If line 2000 et al should be the first line of the next segment (not the
last line of the previous one), you need

sed '1,1999!d' original > result1
sed '2000,4999!d' original > result2
sed '5000,7999!d' original > result3
sed '8000,10000!d' original > result4

And if the file's really big, you might want to have sed exit after it
prints the last line:

sed -e '1,1999!d' -e 2000q original > result1
sed -e '2000,4999!d' -e 5000q original > result2
sed -e '5000,7999!d' -8 8000q original > result3
sed '8000,10000!d' original > result4

-- 
I firmly believed we should not march into Baghdad ...To occupy Iraq
would instantly shatter our coalition, turning the whole Arab world
against us and make ... a latter-day Arab hero assigning young soldiers
to a fruitless hunt for a securely entrenched dictator[.] -- GHWB
-----------------------------------------------------------------------
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:23:39 EDT