In the past, I've had to sort files that are too big for sort
. I assume this is also your problem, though if you provide more information, perhaps we can better diagnose your issue.
The solution to my issue was to break up the file with grep
as a pre-processor. Take a look at your data to see where the clumps will be. I'm assuming it is decently spread out over the alphanumeric space, but I'll discuss how to deal with clumps later.
for char1 in other {0..9} {a..z}; do
out="/tmp/sort.$char1"
echo "Extracting lines starting with '$char1'"
if [ "$char1" = "other" ]; then char1='[^a-z0-9]'; fi
grep -i "^$char1" *.txt > "$out"
sort -u "$out" >> output.txt || break
done
(This uses a bashism. To avoid it, explicitly name each of the 37 characters, like for char1 in other 0 1 2 3 4 5 6 7 8 9 0 a b c d e f ...
)
Clumps: It is completely possible that some of these looped sort
calls will segfault due to having too much data. Merely revise that iteration to be more broken into parts. This might be as simple as removing grep's -i
flag and calling out each capital letter (don't forget to change the other
to [^a-zA-Z0-9]
), or it might require digging into the data. If this is a list of software packages, you may have too many lines that start with "lib" and so the /tmp/sort.l
iteration will fail. The || break
part of this loop will stop processing at this point so you can fix it and resume where you left off. Following the "lib" example, you might want to resume with something like this:
for char1 in 'l[a-h]' 'lia' lib{a..z} lib{0..9} 'li[c-z]' 'l[j-z]' {m..z}; do
...
This breaks the l
list into parts before and after the lib* portion. It's a bit ugly, but it should work. Just pay attention to the desired order so you can preserve it.
df -h tmp