Skip to content

Instantly share code, notes, and snippets.

@rkaneko
Last active December 19, 2018 14:48
Show Gist options
  • Save rkaneko/705e3c805e27de071f04a130269f0e80 to your computer and use it in GitHub Desktop.
Save rkaneko/705e3c805e27de071f04a130269f0e80 to your computer and use it in GitHub Desktop.
awk sample for mapping some column values in some .csv file

awk command sample for mapping some column values in some .csv file

$ cat origin.csv
id,created,notes
1,1518393600000,blahblah
2,1518393600000,foobar

# Case1: We want to all column2 values (e.g. unix timestamp-ish value) to formatted datetime
$ head -1 origin.csv > /dev/stdout && tail origin.csv -n+2 | awk -F "," '{gsub($2, strftime("%Y-%m-%d %H:%M:%S", substr($2, 0, 10))); print}' >> /dev/stdout
id,created,notes
1,2018-02-12 00:00:00,blahblah
2,2018-02-12 00:00:00,foobar

Notes

# cat only header row
$ head -1 origin.csv > /dev/stdout
id,created,notes

# cat only body rows
$ tail origin.csv -n+2
1,1518393600000,blahblah
2,1518393600000,foobar

# replace column2 unix timestamp value to formatted datetime value
$ echo "1,1518393600" | awk -F "," '{gsub($2, strftime("%Y-%m-%d %H:%M:%S", $2)); print}'
1,2018-02-12 00:00:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment