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?
Best Answer
You could use gawk which has the strftime and mktime functions (https://www.gnu.org/software/gawk/manual/html_node/Time-Functions.html).
In more details:
With a basic awk, you need to call the command and read its result with getline:
In more details: