#! /bin/bash
#
# BASH script to parse SGE accounting file and 
# generate SQL statements, one per job
#
# Gowtham (g@mtu.edu)
# Information Technology Services
# Michigan Technological University
# Houghton, MI 49931

# Delete the first four lines (header)
# Delete all empty lines
# Remove leading & trailing white space (all kinds)
# Sort the entries by job_number
# Delete all lines with one character or less
# Insert 'INSERT IGNORE INTO `database_name`.`sge_accounting` VALUES ("", "' 
#        at the beginning of each line
# Replace every ':' with '", "' in every line
# Insert '");' at the end of each line
sed '1,4d' accounting | \
  sed '/^$/d' | \
  sed 's/^[ \t]*//;s/[ \t]*$//' | \
  sort -t: -k6 -n | \
  sed 's/^/INSERT IGNORE INTO `database_name`.`sge_accounting` VALUES ("", "/' | \
  sed 's/:/", "/g' | \
  sed 's/$/");/' > sge_accounting.sql
