linux - print - awk unix
Incrementar la fecha con AWK durante algunos días y meses (2)
Aquí hay una forma de usar GNU awk
. Primero deberá convertir sus requisitos en segundos. Alternativamente, escriba algo de awk
para hacer esto. Esto debería ponerlo en funcionamiento:
awk -f script.awk test.csv
Contenido de 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
}
Resultados:
"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"
Resultados:
"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"
Fecha de incremento con AWK
Hola a todos, necesito tu ayuda. Tengo un archivo "test.csv" y quiero aumentar las fechas de "6 meses" y "10 días" para obtener el siguiente resultado:
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"
Salida 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"
Salida 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"
Lo he intentado con estos comandos:
awk -F "/" , / " " ''{{cmd = "date /" +% Y-% m-% d% T / "-d /" "$ 3" +10 días / ""; cmd | getline datum; cerrar (cmd); print $ 0 ", /" "datum" / ""}} ''test.csv> test-increment-10days.csv
awk -F "/" , / " " ''{{cmd = "date /" +% Y-% m-% d% T / "-d /" "$ 3" +6 meses / ""; cmd | getline datum; cerrar (cmd); print $ 0 ", /" "datum" / ""}} ''test.csv> test-increment-6months.csv
pero estoy obteniendo resultados incorrectos.
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"
Las fechas en "test-increment-10days.csv" solo se incrementan un día y en "test-increment-6months.csv" solo un mes. Espero que alguien pueda ayudar.
Prueba esto:
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
Un ejemplo del comando de fecha subyacente es:
date -d "$(date -d ''2013-05-07 16:02:07'')+10days" "+%Y-%m-%d %T"
Hay dos comandos de date
. La date
interna crea una fecha con un valor especificado. Luego agrégalo 10 días. La fecha externa crea una nueva fecha incrementada y la formatea.