Skip to main content
Tweeted twitter.com/StackUnix/status/937412065744048130
changed week days abbreviation to english
Source Link
nath
  • 6.1k
  • 13
  • 51
  • 94

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:
MiWed, 29.11.2017
FrFri, 01.12.2017
SaSat, 02.12.2017

so my final result would look like:

On MiWed, 29.11.2017 you spend: 1 hour(s)
On FrFri, 01.12.2017 you spend: 1 hour(s)
On SaSat, 02.12.2017 you spend: 3 hour(s)

is it possible to invoke date within the awk command to format the 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:
Mi, 29.11.2017
Fr, 01.12.2017
Sa, 02.12.2017

so my final result would look like:

On Mi, 29.11.2017 you spend: 1 hour(s)
On Fr, 01.12.2017 you spend: 1 hour(s)
On Sa, 02.12.2017 you spend: 3 hour(s)

is it possible to invoke date within the awk command to format the 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?

Source Link
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:
Mi, 29.11.2017
Fr, 01.12.2017
Sa, 02.12.2017

so my final result would look like:

On Mi, 29.11.2017 you spend: 1 hour(s)
On Fr, 01.12.2017 you spend: 1 hour(s)
On Sa, 02.12.2017 you spend: 3 hour(s)

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