![在 awk 命令中调用日期以格式化输出](https://linux22.com/image/122966/%E5%9C%A8%20awk%20%E5%91%BD%E4%BB%A4%E4%B8%AD%E8%B0%83%E7%94%A8%E6%97%A5%E6%9C%9F%E4%BB%A5%E6%A0%BC%E5%BC%8F%E5%8C%96%E8%BE%93%E5%87%BA.png)
我有一个csv
以下格式的文件:
20171129,1
20171201,0.5
20171201,0.5
20171202,1.25
20171202,1.75
如果第二个字段与此命令遵循相同的日期,我将使用以下命令对第二个字段进行求和:
awk -F ',' '{a[$1] += $2} END{for (i in a) print "On " i, "you spend: "a[i] " hour(s)"}' << "file.csv"
我得到的输出如下所示:
On 20171129 you spend: 1 hour(s)
On 20171201 you spend: 1 hour(s)
On 20171202 you spend: 3 hour(s)
我现在想要实现的是格式化日期,就像我想的那样:
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
所以我的最终结果如下:
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)
是否可以date
在命令中调用awk
来格式化输出?
答案1
你可以使用呆呆地其中有时间和时间功能 (https://www.gnu.org/software/gawk/manual/html_node/Time-Functions.html)。
gawk -F ',' '{a[$1] += $2} END{for (i in a) print "On " strftime("%a, %d.%m.%Y", mktime( substr(i,1,4) " " substr(i,5,2) " " substr(i,7,2) " 0 0 0" )) " you spend: "a[i] " hour(s)" }' files.csv
更详细地说:
gawk -F ',' '
{
a[$1] += $2
}
END{
for (i in a) {
# mktime needs a date formated like this "2017 12 31 23 59 59"
# strftime needs a Unix timestamp (produced by mktime)
print "On " strftime("%a, %d.%m.%Y", mktime( substr(i,1,4) " " substr(i,5,2) " " substr(i,7,2) " 0 0 0" )) " you spend: "a[i] " hour(s)"
}
}' files.csv
有了基本的awk,您需要调用该命令并读取其结果获取线:
awk -F ',' '{a[$1] += $2} END{ for (i in a) { COMMAND = "date +\"%a, %d.%m.%Y\" -d " i ; if ( ( COMMAND | getline DATE ) 0 ) { print "On " DATE " you spend: "a[i] " hour(s)" } ; close(COMMAND) } }' files.csv
更详细地说:
awk -F ',' '
{
a[$1] += $2
}
END{
for (i in a) {
# Define command to call
COMMAND = "date +\"%a, %d.%m.%Y\" -d " i
# Call command and check that it prints something
# We put the 1st line of text displayed by the command in DATE
if ( ( COMMAND | getline DATE ) > 0 ) {
# Print the result
print "On " DATE " you spend: "a[i] " hour(s)"
}
# Close the command (important!)
# Your child process is still here if you do not close it
close(COMMAND)
}
}' files.csv