Skip to content

Instantly share code, notes, and snippets.

@Hermann-SW
Created Sep 15, 2021
Embed
What would you like to do?
Parallel recursive bash mergesort
#!/bin/bash
tms="/tmp/ms.$1"
rm -f ${tms}[01]
IFS=''
while read line1
do
let n1=n1+1; echo $line1 >> ${tms}0
if read line2; then let n2=n2+1; echo $line2 >> ${tms}1; fi
done
tsm="/tmp/sm.$1"
if [[ n1 -gt 1 ]]; then (cat ${tms}0 | $0 ${1}0)& else cp ${tms}0 ${tsm}0& fi
pid1=$!
if [[ n2 -gt 1 ]]; then (cat ${tms}1 | $0 ${1}1)& else cp ${tms}1 ${tsm}1& fi
pid2=$!
wait $pid1 $pid2
# echo "merge(${tsm}0,${tsm}1)"
cmd="comm ${tsm}0 ${tsm}1 | sed \"s/^\t\t\(.*\)$/\10xa\1/; s/^\t//\""
if [[ "$1" != "" ]]; then out=" > /tmp/sm."$1; fi
eval $cmd $out
rm /tmp/ms.${1}[01] /tmp/sm.${1}[01]
@Hermann-SW

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment