Skip to content

Instantly share code, notes, and snippets.

@kiang
Created May 14, 2026 13:08
Show Gist options
  • Select an option

  • Save kiang/9180a2348f022403bb59928b69b1128a to your computer and use it in GitHub Desktop.

Select an option

Save kiang/9180a2348f022403bb59928b69b1128a to your computer and use it in GitHub Desktop.
#!/bin/bash
# Replace the header of an upload file with the template header
TEMPLATE="APP020001_income.csv"
if [ -z "$1" ]; then
echo "Usage: $0 <upload_file>"
exit 1
fi
if [ ! -f "$1" ]; then
echo "File not found: $1"
exit 1
fi
if [ ! -f "$TEMPLATE" ]; then
echo "Template not found: $TEMPLATE"
exit 1
fi
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
# Remove BOM if present
"$SCRIPT_DIR/remove_bom.sh" "$1"
# Convert CRLF to LF
sed -i 's/\r$//' "$1"
# Get template header (first line)
HEADER=$(head -n 1 "$TEMPLATE")
# Replace first line with template header, keep data rows, remove surrounding double quotes
tail -n +2 "$1" | sed 's/^"//; s/"$//; s/","/,/g' | cat <(echo "$HEADER") - > "$1.tmp"
mv "$1.tmp" "$1"
echo "Fixed: header replaced, double quotes removed in $1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment