Last active
September 20, 2018 12:46
-
-
Save linosteenkamp/0c89303141dfa64a7e39518a2acdc803 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/bin/bash | |
# Add md5 hash to the end every line in a CSV file | |
# Usage ./hash_csv_records.sh input.csv > output.csv | |
input="${1:-/dev/stdin}" | |
SEP=";" | |
if [[ $(uname) == 'Linux' ]]; then | |
alias md5='md5sum' | |
fi | |
[ -z "${input}" ] && printf "No CSV input file specified" && exit 1 | |
[ ! -e "${input}" ] && printf "Unable to locate ${input}" && exit 1 | |
data=$(sed '/^$/d' "${input}") | |
line_count=$(printf "${data}" | wc -l) | |
first_row=true | |
while IFS=$'\n\r' read -r line; do | |
if [[ ${first_row} = true ]]; then | |
printf "${line}\n" | |
first_row=false | |
else | |
if [[ $(uname) == 'Linux' ]]; then | |
hash=$(echo ${line} | md5sum | awk '{ print $1}') | |
else | |
hash=$(echo ${line} | md5 | awk '{ print $1}') | |
fi | |
printf "${line}${SEP}${hash}\n" | |
fi | |
done <<< "${data}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment