Skip to main content
2 of 2
changed week days abbreviation to english
nath
  • 6.1k
  • 13
  • 51
  • 94

Invoke date within an awk command to format output

I have a csv file in the following format:

20171129,1
20171201,0.5
20171201,0.5
20171202,1.25
20171202,1.75

I use the following command to sum up the second field if it is following the same date with this command:

awk -F ',' '{a[$1] += $2} END{for (i in a) print "On " i, "you spend: "a[i] " hour(s)"}' << "file.csv"

the output I get looks like this:

On 20171129 you spend: 1 hour(s)
On 20171201 you spend: 1 hour(s)
On 20171202 you spend: 3 hour(s)

what I want to achieve now is format the date as if I would with:

awk -F ',' '{a[$1]} END{for (i in a) print i}' << "file.csv" \
 | date +"%a, %d.%m.%Y" -f -

# prints:
Wed, 29.11.2017
Fri, 01.12.2017
Sat, 02.12.2017

so my final result would look like:

On Wed, 29.11.2017 you spend: 1 hour(s)
On Fri, 01.12.2017 you spend: 1 hour(s)
On Sat, 02.12.2017 you spend: 3 hour(s)

is it possible to invoke date within the awk command to format the output?

nath
  • 6.1k
  • 13
  • 51
  • 94