Skip to content

Instantly share code, notes, and snippets.

@tecmaverick
Created December 15, 2022 00:32
Show Gist options
  • Save tecmaverick/7ae6002f5ba861d496f8bc701e0a1e30 to your computer and use it in GitHub Desktop.
Save tecmaverick/7ae6002f5ba861d496f8bc701e0a1e30 to your computer and use it in GitHub Desktop.
# Script to add a header and static values to CSV file
# Header
awk '{if(NR==1){$0="env,"$0; print $0} ;if(NR!=1){print $0}}' input.csv > output.csv
# Add rows
awk -F"," 'BEGIN { OFS = "," } {$1="2012-02-29 16:13:00"; print}' input.csv > output.csv
awk -F"," 'BEGIN { OFS = "," } {$0="XXXXX,"$0; print $0}' input.csv > output.csv
awk -F"," '{if (NR>1) { OFS = ",";{$1="XXX"; print}};if(NR==1){print}}' input.csv > output.csv
HEADER_NAME=$1
FIELD_VAL=$2
INPUT_FILENAME=$3
OUPUT_FILENAME=$4
awk -v HEADER_NAME=$1 -v FIELD_VAL=$2 -F"," '{if(NR==1){$0=HEADER_NAME","$0; print $0};if (NR>1) { OFS = ",";{$0=FIELD_VAL","$0; print $0}}}' $INPUT_FILENAME > $OUPUT_FILENAME
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment