Skip to content

Instantly share code, notes, and snippets.

@tecmaverick
Created January 20, 2023 04:36
Show Gist options
  • Save tecmaverick/09ef2c02aeaf66e2f4e8bf966319fdb9 to your computer and use it in GitHub Desktop.
Save tecmaverick/09ef2c02aeaf66e2f4e8bf966319fdb9 to your computer and use it in GitHub Desktop.
Insert New Header and Value to CSV file, and shuffle fields
#To execute script
# ./script.sh MyHeader MyCellValue 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;};if (NR>1) { OFS = ",";{$0=FIELD_VAL","$0; }};if(NR>=0){FS=OFS=",";{f1=$1;f2=$2;f3=$3;f4=$4;f5=$5;$1=f2;$2=f1;$3=f3;$4=f4;$5=f5;print}}}' $INPUT_FILENAME > $OUPUT_FILENAME
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment