Last active
March 30, 2023 14:24
-
-
Save marshki/9568d9acd96620d5b6db4bf1bac91a2a to your computer and use it in GitHub Desktop.
Create text files with parsed data, suitable for import into Google Groups.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# | |
# extractor | |
# | |
# Create text files with parsed data, suitable for import into Google Groups. | |
# | |
# Author: M. Krinitz <mjk235 [at] nyu [dot] edu> | |
# Date: 2021.05.15 | |
# License: MIT | |
####################### | |
# Note on mailing lists | |
####################### | |
# "Ideal case" mailing list takes the form: | |
# # list-o-steves | |
# # | |
# Stephen Gary Wozniak <[email protected]> | |
# Stephen William Hawking <[email protected]> | |
# Steven Anthony Ballmer <[email protected]> | |
# Steve Austin <[email protected]> | |
# Steven Paul Jobs <[email protected]> | |
# Stevland Hardaway Morris <[email protected]> | |
# For reference: | |
# https://support.google.com/a/answer/9400087?hl=en&visit_id=638157825756116575-2877305235&rd=1#zippy=%2Cupload-users-from-a-csv-file | |
########### | |
# Variables | |
########### | |
domain="example.com" | |
file_type="csv" | |
timestamp=$(date "+%b %d %X") | |
########### | |
# Functions | |
########### | |
# Make file | |
# e.g.: ste.filetype | |
# Headers | |
# Row 1: Group email | |
# e.g.: [email protected] | |
# Row 2: Column headers | |
# e.g.: Group Email [Required], Member Email, Member Type, Member Role, Member Name | |
make_file () { | |
printf "%s\n" "creating: $file.$file_type with headers..." | |
printf "%s\n" "$file@$domain" > "$file"."$file_type" | |
printf "%s\n" "Group Email [Required], Member Email, Member Type, Member Role, Member Name" >> "$file"."$file_type" | |
} | |
# Parse list | |
# grep to ignore lines starting with: '#' | |
# pipe to awk | |
# awk set offset character to comma (,) | |
# then use field separator " " (one (1) blank space) | |
# then add: one (1) blank space, last column, add USER, add MEMBER, add first column | |
# e.g.: ,<[email protected]>,USER,MEMBER,Steve | |
parse_list () { | |
printf "%s\n" "parsing list: $file..." | |
grep "^[^#]" "$file" | awk -v OFS=, -F " " '{print "", $NF, "USER", "MEMBER", $1}' >> "$file"."$file_type" | |
} | |
# Iterate | |
# If file in pwd is directory or symbolic link, skip | |
# else do the dew. | |
iterate () { | |
for file in *; do | |
if [[ -d "$file" || -L "$file" ]]; then | |
printf "%s\n" "The item: $file is a directory or symbolic link, skipping..." | |
else [[ -f "$file" ]] | |
printf "%s\n" "The item: $file is a file, continuing..." | |
filter_list | |
make_file | |
parse_list | |
fi | |
done | |
} | |
# Exit status check | |
# If non-zero (0) exit status, DANGER | |
exit_status () { | |
if [[ $retVal -ne 0 ]]; then | |
printf "%s\n" "ACHTUNG!!! Something went wrong, homie." | |
else | |
printf "%s\n" "Done. Exiting @ $timestamp." | |
fi | |
} | |
# Main function | |
main () { | |
printf "%s\n" "Running: extractor.sh @ $timestamp." | |
iterate | |
} | |
main "$@" | |
retVal=$? | |
exit_status |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment