Skip to content

Instantly share code, notes, and snippets.

@sjnovick
Created July 3, 2019 17:54
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 sjnovick/a6fa3b7ee7d17cffc94a196e2427e3e6 to your computer and use it in GitHub Desktop.
Save sjnovick/a6fa3b7ee7d17cffc94a196e2427e3e6 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# essay.sh
#
# Bash script to find, extract, cleanup, and deliver
# graduate admissions essays from new student applications.
#
# sjnovick 2015
#
# paths
REPORTDIR="/home/user/reports"
REPORTDEST="/mnt/OfficeShare/reports/"
SPEEDEHOMEDIR="/home/user"
ARCHIVEDIR="/home/user/archive"
LOGFILE="/home/user/essay.log"
# app name variable
APP="APPS*"
# string to determine if essay is present
STRING="ESSAY INCLUDED"
# redirect STDERR to STDOUT and append both to LOGFILE
exec 1>> $LOGFILE 2>&1
# banter for log file
printf "***** Starting: $(date)\n" >> $LOGFILE
#echo "***** Starting: $(date)"
#set counter
z=0
# loop for all apps present
for j in $SPEEDEHOMEDIR/$APP
do
# count
let "z++"
# archive the current app before we get started
cp $j $ARCHIVEDIR
# determine if essay(s) exist in current app file
grep -q "$STRING" $j
if [ $? -eq 0 ]
then
printf "Essays found in $j\n" >> $LOGFILE
### BEGIN REPORT ROUTINE
DATE2="$(date +"%m-%d-%y_%H%M%S")"
REPORT="$REPORTDIR/$DATE2.txt"
# write neat report header
printf "" > $REPORT
printf "******************************\n" >> $REPORT
printf "START OF REPORT\n" >> $REPORT
printf "$DATE2\n" >> $REPORT
printf "Source File:\n" >> $REPORT
printf "$j\n" >> $REPORT
printf "******************************\n\n" >> $REPORT
# set counter
n=0
# array declaration
declare -a ARRAY
# set internal field separator to new line
IFS=$'\n'
# populate array, line by line, with apps that contain essays
ARRAY=( $(grep -Pzo "ST!189![\s\S]*?ESSAY INCLUDED[\s\S]*?SE!.*" $j) )
# loop through every line
for i in ${ARRAY[@]}
do
# check to see if this line is the start of an app
echo $i | grep -q "ST!189!"
if [ $? -eq 0 ]
# start of an app
then
# write neat app header to report
printf "************\n" >> $REPORT
printf "START OF APP\n" >> $REPORT
printf "************\n" >> $REPORT
# drop line
i=$(echo $i | sed '/ST!189!/d')
printf "$i\n" >> $REPORT
# count
let "n++"
# not the start of an app
else
# drop unwanted characters from line
# sed reference:
# sed 's/old/new/' = replace old string with new string
# sed '/string/d' = drop entire line that matches string
i=$(echo $i | sed \
-e '/N1!TM!/d' \
-e '/N1!AT!/d' \
-e '/REF!SY!/d' \
-e '/IN1!/d' \
-e '/DANGEROUS CHARACTERS FOUND/d' \
-e '/MSG!DY!/d' \
-e '/SE!/d' \
-e 's/BGN!00!.........!//' \
-e 's/!1812!CT//' \
-e 's/REF!48!//' \
-e 's/DTP!196!CM!//' \
-e 's/IN2!05!//' \
-e 's/IN2!02!//' \
-e 's/IN2!03!//' \
-e 's/IN2!09!//' \
-e 's/DMG!D8!//' \
-e 's/RQS!!!//' \
-e 's/!!!0!!8//' \
-e 's/MSG!//' \
-e 's/<br\s\/>//' \
-e 's/<br\s\/>//')
# write line to report
printf "$i\n" >> $REPORT
fi
done
# write neat report footer
printf "*************\n" >> $REPORT
printf "TOTAL APPS: $n\n" >> $REPORT
printf "**************\n" >> $REPORT
# remove trailing backslashes and empty lines from report
sed -i -e 's/\\//' -e '/^$/d' -e '/^\s*$/d' $REPORT
#sed -i '/^$/d' $REPORT
# convert report to dos format
unix2dos -q $REPORT
#copy report to filer
cp $REPORT $REPORTDEST
### END REPORT ROUTINE
# delete essays from current app file
sed -i '/^RQS!!!ESSAY.*$/,/^SE.*$/ {/^SE.*$/!d}' "$j"
else
printf "No essays found in $j\n" >> $LOGFILE
fi
done
printf "Applications processed: $z\n" >> $LOGFILE
printf "Essays processed: $n\n" >> $LOGFILE
printf "***** Done: $(date)\n" >> $LOGFILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment