Skip to content

Instantly share code, notes, and snippets.

@linosteenkamp
Last active September 20, 2018 12:46
Show Gist options
  • Save linosteenkamp/0c89303141dfa64a7e39518a2acdc803 to your computer and use it in GitHub Desktop.
Save linosteenkamp/0c89303141dfa64a7e39518a2acdc803 to your computer and use it in GitHub Desktop.
#!/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