AWK的增加日期数天和数月 [英] Increment date with AWK for few days and months

查看:71
本文介绍了AWK的增加日期数天和数月的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用AWK增加日期

大家好,我需要您的帮助.我有一个文件"test.csv",我想增加"6个月"和"10天"的日期,以便获得以下输出:

Hi all, I need your help. I have a file "test.csv" and I want to increase the dates for "6 months" and "10 days" so that I am getting the following output:

test.csv:

"000001","TEST1","2013-05-07 16:02:07","ACTIVE"
"000003","TEST3","2013-05-09 16:02:07","ACTIVE"
"000004","TEST4","2013-05-10 16:02:07","ACTIVE"
"000005","TEST5","2013-05-11 12:02:07","ACTIVE"

输出test-increment-10days.csv:

"000001","TEST1","2013-05-07 16:02:07","ACTIVE","2013-05-17 16:02:07"
"000003","TEST3","2013-05-09 16:02:07","ACTIVE","2013-05-19 16:02:07"
"000004","TEST4","2013-05-10 16:02:07","ACTIVE","2013-05-20 16:02:07"
"000005","TEST5","2013-05-11 12:02:07","ACTIVE","2013-05-21 12:02:07"

输出test-increment-6months.csv:

"000001","TEST1","2013-05-07 16:02:07","ACTIVE","2013-11-07 16:02:07"
"000003","TEST3","2013-05-09 16:02:07","ACTIVE","2013-11-09 16:02:07"
"000004","TEST4","2013-05-10 16:02:07","ACTIVE","2013-11-10 16:02:07"
"000005","TEST5","2013-05-11 12:02:07","ACTIVE","2013-11-11 12:02:07"

我已使用以下命令尝试过此操作:

awk -F"\" ,\""'{{cmd ="date \" +%Y-%m-%d%T \"-d \""$ 3" + 10天\""; cmd |盖特琳数据关闭(cmd);打印$ 0,\"基准" \"}}'test.csv> test-increment-10days.csv

awk -F"\",\"" '{{ cmd="date \"+%Y-%m-%d %T\" -d \""$3" +10 days\""; cmd | getline datum; close(cmd); print $0 ",\""datum"\""}}' test.csv > test-increment-10days.csv

awk -F"\" ,\""'{{cmd ="date \" +%Y-%m-%d%T \"-d \""$ 3" + 6个月\""; cmd |盖特琳原点;关闭(cmd);打印$ 0,\"基准" \"}}'test.csv> test-increment-6months.csv

awk -F"\",\"" '{{ cmd="date \"+%Y-%m-%d %T\" -d \""$3" +6 months\""; cmd | getline datum; close(cmd); print $0 ",\""datum"\""}}' test.csv > test-increment-6months.csv

但是我得到了错误的输出.

but I am getting this wrong outputs.

test-increment-10days.csv:

"000001","TEST1","2013-05-07 16:02:07","ACTIVE","2013-05-08 16:02:07"
"000003","TEST3","2013-05-09 16:02:07","ACTIVE","2013-05-10 16:02:07"
"000004","TEST4","2013-05-10 16:02:07","ACTIVE","2013-05-11 16:02:07"
"000005","TEST5","2013-05-11 12:02:07","ACTIVE","2013-05-12 12:02:07"

test-increment-6months.csv:

"000001","TEST1","2013-05-07 16:02:07","ACTIVE","2013-06-07 16:02:07"
"000003","TEST3","2013-05-09 16:02:07","ACTIVE","2013-06-09 16:02:07"
"000004","TEST4","2013-05-10 16:02:07","ACTIVE","2013-06-10 16:02:07"
"000005","TEST5","2013-05-11 12:02:07","ACTIVE","2013-06-11 12:02:07"

"test-increment-10days.csv"中的日期仅增加一天,而"test-increment-6months.csv"中的日期仅增加一个月.希望有人能帮忙.

The dates in "test-increment-10days.csv" are only incremented one day and in the "test-increment-6months.csv" only one month. Hope somebody can help.

推荐答案

尝试一下:

awk -F"\",\"" '{cmd="date -d \"$(date -d \""$3"\")+10days\" \"+%Y-%m-%d %T\"";cmd | getline datum; close(cmd); print $0 ",\""datum"\""}' test.csv > test-increment-10days.csv

基础日期命令的一个示例是:

An example of the underlying date command is:

date -d "$(date -d '2013-05-07 16:02:07')+10days" "+%Y-%m-%d %T"

有两个date命令.内部的date创建具有指定值的日期.然后再添加10天.外部日期会创建一个新的递增日期并将其格式化.

There are two date commands. The inner date creates a date with a specified value. Then add 10 days to it. The outer date creates a new incremented date and formats it.

这篇关于AWK的增加日期数天和数月的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆