Skip to content

Instantly share code, notes, and snippets.

@tecmaverick
Last active February 13, 2023 03:46
Show Gist options
  • Save tecmaverick/7b414380771c02c7c069512de5a86f0f to your computer and use it in GitHub Desktop.
Save tecmaverick/7b414380771c02c7c069512de5a86f0f to your computer and use it in GitHub Desktop.
Insert Header to CSV AWK and Bash version
# abc.csv contents
# Alpha, USA, 12
# Beta, USA, 13
# abc.csv contents after adding header
# Name, Country, Age
# Alpha, USA, 12
# Beta, USA, 13
#Using streams - Solution1
value=$(<abc.csv); echo "Name,Country,Age">abc.csv; echo "$value">>abc.csv
#Using streams - Solution2
echo -e "Name,Country,Age\n$(cat abc.csv)">abc.csv
#Using AWK
a=$( awk -F, 'NR==1 {print "Name","Country","Age"}{print $1,$2,$3}' abc.csv | column -t);echo "$a">abc.csv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment