Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Yes, I know it sounds odd. Sequential and Binary-splits don't mix.. That is unless the sequence is the byte offset within the file itself...

I've scrambled together a binary split search in bash script, using dd. It has Search-and-Find times of 3-9 seconds for an 8 GB file.. so it works (but slower than I know it can be)... I'd really prefer to not have to polish this wheel; It just took my fancy to do it as an exercise in bash (having a project is the best way to learn a language, etc). I think this would be pretty straight forward in C/++, etc... I'm curious to see some other examples (particularly bash ones.

share|improve this question
    
By binary split you mean just split in half once, or keep splitting the halves into smaller halves? If you just need it once, probably something along the lines of head -n $(($(cat file | wc -l) / 2)) file for the first half and tail ... for the second half. –  Marcin Mar 9 '11 at 2:05
    
@marcin: Yes that's what I mean. I've tried head/tail -c. It slows down dramatically to 2min for just one access at offset=8GB, and that's without using wc. wc alone takes 2min on 8GB (see 3rd comment here: unix.stackexchange.com/questions/8444/…)... So head/tail split may tehcnically work, but simple awk/sed search is faster.. 'dd's skip avoids most sequentail reads. My max 9sec comes from a file with each Key having 86400 duplicates for which I used a combo of split and seq-reads) –  Peter.O Mar 9 '11 at 3:08
    
Split up the problem into pieces and see which part is the expensive part. If it is the initial line count that makes it slow, see if you can arrive at the number of lines differently. You seem to have some knowledge about the dataset, how many of what values...can you use that to figure out where the middle is? Also, does it have to be the exact middle? If all you need is an approximate middle, read the size (meta-info lookup, no sequential reads at all!), divide it in half, and start printing the size/2 characters. Once you reach that point, then print the other half. –  Marcin Mar 9 '11 at 5:41
    
Marcin, thanks. I think my main problem was that I didn't plan it well enough.. Finding each key was relatively straight forward (1 second per key), but chasing through the duplicates got messy..I located the first/last duplicate-key, by binary-doubling out looking for a non-dupe, and then used a sequential read to find the correct first/last key line.. This bogs down when there are thousands of lines with the same YYYY-MM-DD key (eg logging one line per second.)...If you are interested, here is the script: paste.ubuntu.com/577919 –  Peter.O Mar 9 '11 at 16:46

1 Answer 1

up vote 3 down vote accepted

All the basic text processing utilities are meant to act as filters, and most are meant to process their input as a stream (i.e. read a little input, process it, write the corresponding output, repeat). dd is a little unusual, both by its syntax and by the options it offers. dd is the only shell interface to lseek, and as you've noticed it's clumsy. When you reach this point, it's time to switch to a more powerful scripting language such as Perl or Python.

share|improve this answer
    
Thanks,Gilles... That sounds like good advice.. I've been focusing on bash, but I suspect that it is time to now,"just use it for what it is", and start looking at something more heavy-weight.. I've touched on Python, so I'll probably continue there.. Thanks –  Peter.O Mar 8 '11 at 19:41
1  
Use the bisect module in Python to do this. –  Michael Dillon Mar 9 '11 at 0:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.