使用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 | getline datum;关闭(CMD); print $ 0“,”“”datum“\”“}}'test.csv>测试增量-10days.csv
awk -F“\”,\“”'{{cmd =“date \”+%Y-%m-%d%T \“ - d \”“$ 3”+ 6个月\””; cmd | getline datum;关闭(CMD); print $ 0“,”“”datum“\”“}}'test.csv>测试增量-6months.csv
但我得到了错误的输出。
测试递增-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"
测试递增-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”中的日期仅增加一个月。希望有人可以提供帮助。
答案 0 :(得分:3)
试试这个:
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天。外部日期创建一个新的递增日期并对其进行格式化。
答案 1 :(得分:3)
这是使用GNU awk
的一种方式。您首先需要将您的需求转换为秒。或者,写一些awk
来执行此操作。这应该让你开始运行:
awk -f script.awk test.csv
script.awk
的内容:
BEGIN {
FPAT = "([^,]+)|(\"[^\"]+\")"
OFS=","
}
{
string = "\"(....)-(..)-(..) (..):(..):(..)\""
format = "\\1 \\2 \\3 \\4 \\5 \\6"
date_spec = gensub(string, format, "", $3)
timestamp = mktime(date_spec)
# 10 days = 864000 seconds
# 6 months = 15638400 seconds
# uncomment one of the following:
# new_stamp = "\"" strftime("%F %T", timestamp + 864000) "\""
# new_stamp = "\"" strftime("%F %T", timestamp + 15638400) "\""
print $0, new_stamp
}
结果:
"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"
结果:
"000001","TEST1","2013-05-07 16:02:07","ACTIVE","2013-11-04 16:02:07"
"000003","TEST3","2013-05-09 16:02:07","ACTIVE","2013-11-06 16:02:07"
"000004","TEST4","2013-05-10 16:02:07","ACTIVE","2013-11-07 16:02:07"
"000005","TEST5","2013-05-11 12:02:07","ACTIVE","2013-11-08 12:02:07"
答案 2 :(得分:0)
使用较少引用并使用简化的awk 'BEGIN {
FPAT = "([^,]+)|(\"[^\"]+\")"
OFS=","
}
{
gsub(/["]/,"",$3);
cmd = "date -d \""$3" 10 days\" \"+%Y-%m-%d %I:%M:%S %p\""
}
{
cmd|getline D; close(cmd);
$3="\042"D"\042"
}1' file
构造的dogbane's answer的替代版本将是
awk 'BEGIN {
FPAT = "([^,]+)|(\"[^\"]+\")"
OFS=","
}
{
gsub(/["]/,"",$3);
cmd = "date -d \""$3" 6 months\" \"+%Y-%m-%d %I:%M:%S %p\""
}
{
cmd|getline D; close(cmd);
$3="\042"D"\042"
}1' file
将持续6个月
cmd
所做的增强是:
getline
时使用较少的引号,这使得在awk
的{{1}}函数中使用起来很优雅。date -d<date-string> 10 days
运算符即可使用+
。FPAT
正则表达式构造以更严格地解析各个CSV字段。