2013-06-05 93 views
3

增量日期AWK增量日期爲AWK幾天,幾個月

大家好,我需要你的幫助。我有一個文件「test.csv」我希望增加的日期中「六個月」和「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" 

輸出測試遞增-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" 

輸出測試遞增-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 = 「日期\」 +%Y-%間 - % d%T \「-d \」「$ 3」+10天\「」; cmd | getline數據;關閉(CMD);打印$ 0「,\」「datum \」「}}'test.csv> test-increment-10days.csv

awk -F」\「,\」「'{{cmd =」date \ 「+%Y-%m-%d%T \」-d \「」$ 3「+6 months \」「; cmd | getline數據;關閉(CMD);打印$ 0「,\」「datum」\「」}}'test.csv> test-increment-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" 

在 「測試增量-10days.csv」 的日期只增加一天,而在「test-increment-6months.csv」只有一個月。希望有人能幫助。

回答

2

嘗試這種情況:

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天。外部日期會創建一個新的遞增日期並對其進行格式化。

+0

THX羅布麻,你的答案是工作完善。 – user2450752

1

下面是使用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" 
+0

+1使用FPAT和內置函數。由於FPAT,GAWK版本4或更高版本將工作。 –

相關問題