tame/build-aux/csvm2csv

111 lines
2.8 KiB
Plaintext
Raw Normal View History

#!/bin/bash
2018-05-04 11:17:47 -04:00
# Compiles a "magic" CSV file into a normal CSV
#
# Copyright (C) 2014-2019 Ryan Specialty Group, LLC.
2018-05-04 11:17:47 -04:00
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# For format of CSVMs, see `csvm-expand'.
2018-05-04 11:17:47 -04:00
#
# To disable sorting of CSVM output, use the `!NOSORT' directive before the
# header line.
2018-05-04 11:17:47 -04:00
##
# account for symlinks, since historically this script lives in a different
# directory and has been symlinked for compatibility
declare -r mypath=$( dirname "$( readlink -f "$0" )" )
2018-05-04 11:17:47 -04:00
# Generate -k arguments for GNU sort given a CSV header
#
# The generated arguments will be of the form -k1,1n ... -kl,ln, where `l'
# is the total number of header entries.
#
# For example, given this header:
# foo, bar, baz
# the output would be:
# -k1,1n -k2,2n -k3,3n
sort-key-args()
{
local -r header="${1?Missing CSV header}"
local -i i=0
# generate -ki,in for each column (notice that a trailing
# comma is added to the header because of the read delimiter)
while read -d,; do
echo -n "-k$((++i)),${i}n "
done <<< "$header,"
}
# Sort every column of CSV
#
# The columns will all be sorted left-to-right. The header is left in place
# as the first row.
csv-sort()
2018-05-04 11:17:47 -04:00
{
# the first line of the expanded CSVM is the CSV header
local header; read -r header
local -r keys=$( sort-key-args "$header" )
# all remaining input (which is now sans header) is sorted
echo "$header"
sort -t, $keys -
2018-05-04 11:17:47 -04:00
}
# Output usage information
#
# Kudos to you if you understand the little Easter egg.
usage()
{
cat <<EOU
Usage: $0 [FILE]
Expand CSVM represented by FILE or stdin into a CSV
2018-05-04 11:17:47 -04:00
The columns of the expanded CSV will be automatically sorted
left-to-right. To inhibit this behavior, use the \`!NOSORT'
directive anywhere before the header line in the source CSVM.
2018-05-04 11:17:47 -04:00
Options:
--help Output usage information.
2018-05-04 11:17:47 -04:00
This program has magic CSV powers.
EOU
exit 64 # EX_USAGE
2018-05-04 11:17:47 -04:00
}
# Sort CSV rows left-to-right unless the `!NOSORT' directive is provided
main()
2018-05-04 11:17:47 -04:00
{
test ! "$1" == --help || usage
"$mypath/csvm-expand" "$@" \
| {
local directives; read -r directives
# ignore sorting if given NOSORT directive
if [[ "$directives" =~ NOSORT ]]; then
cat
else
csv-sort "$sort"
fi
}
2018-05-04 11:17:47 -04:00
}
main "$@"