在 awk 命令中调用日期以格式化输出

在 awk 命令中调用日期以格式化输出

我有一个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 

相关内容