Skip to content

Instantly share code, notes, and snippets.

@Hermann-SW
Created September 15, 2021 19:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hermann-SW/5a594f053809a3d395f740a5cccada44 to your computer and use it in GitHub Desktop.
Save Hermann-SW/5a594f053809a3d395f740a5cccada44 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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