Skip to content

Instantly share code, notes, and snippets.

@masashinakata
Last active October 27, 2017 03:58
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 masashinakata/93a3c77045a5aa0c59f31782ea8ae71c to your computer and use it in GitHub Desktop.
Save masashinakata/93a3c77045a5aa0c59f31782ea8ae71c to your computer and use it in GitHub Desktop.
unpack.sh - Snippet that splits into sample files and answer files.
#!/bin/sh
i=1
bodyname="sample"
test -f "$bodyname$i.txt" && rm -f "$bodyname$i.txt"
test -f "$1" && exec <"$1"
sed -ne '
s/^ *$//
H
$ {
g
s/^\n*//g
s/\n*$//g
s/\(\n\n\)\n*/\1/g
p
}
' | while read l; do
if ! echo "$l" | grep '^ *$' > /dev/null; then
echo $l >> "$bodyname$i.txt"
else
if [ "$bodyname" == "sample" ]; then
bodyname="answer"
else
bodyname="sample"
i=`expr $i + 1`
fi
test -f "$bodyname$i.txt" && rm -f "$bodyname$i.txt"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment