一尘不染

用AWK递增日期数天和数月

linux

AWK的增加日期

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

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” +6个月\“”; cmd
| 盖特琳原点; 关闭(cmd); 打印$ 0“,\”“基准” \“”}}’test.csv> test-increment-6months.csv

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

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”中仅增加一个月。希望有人能帮忙。


阅读 262

收藏
2020-06-03

共1个答案

一尘不染

尝试这个:

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

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

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

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

2020-06-03