[英]Change date format in a file with awk
var1= date -d "19521029 1010" +"%Y-%m-%d %H:%M"
echo$var1
its working properly but date -d
inside awk is not working它工作正常,但 awk 中的
date -d
不起作用
file文件
KOD19|KAD37748|DEL37728|VIDYA|19521029 1010|201209111625
SASI19|NAS38228|DEL37728|KARTHIKA|19521029 0000|201308071912
RADHA94|VAS37748|DEL37728|LALINKA|19521029 0000|201407061815
First method第一种方法
awk 'BEGIN {FS=OFS="|"} $5=date -d "$5"+"%Y-%m-%d %H:%M" {print}' file
second method第二种方法
awk -F '|' '{$5=date -d $5+"%Y-%m-%d %H:%M"; {OFS="|"}; print}' file
Desired output期望输出
KOD19|KAD37748|DEL37728|VIDYA|1952-10-29 10:10|201209111625
SASI19|NAS38228|DEL37728|KARTHIKA|1952-10-29 10:10|201308071912
RADHA94|VAS37748|DEL37728|LALINKA|1952-10-29 10:10|201407061815
I want to convert the fifth column of "file" to user input date format.我想将“文件”的第五列转换为用户输入日期格式。 Actually column number and date formats are dynamic ie dt="%Y-%m-%d %H:%M" and num=$5 it will very depends on user requirement.
实际上列号和日期格式是动态的,即 dt="%Y-%m-%d %H:%M" 和 num=$5 这将非常取决于用户的要求。
Just use match()
to catch the data in the 5th field and print it back:只需使用
match()
捕获第 5 个字段中的数据并将其打印回来:
awk -F"|" -v OFS="|" '{
match($5, /([0-9]{4})([0-9]{2})([0-9]{2}) ([0-9]{2})([0-9]{2})/, a);
$5=sprintf("%d-%d-%d %s:%s", a[1], a[2], a[3], a[4], a[5])
}1' file
It returns:它返回:
$ awk -F"|" -v OFS="|" '{match($5, /([0-9]{4})([0-9]{2})([0-9]{2}) ([0-9]{2})([0-9]{2})/, a); $5=sprintf("%d-%d-%d %s:%s", a[1], a[2], a[3], a[4], a[5])}1' file
KOD19|KAD37748|DEL37728|VIDYA|1952-10-29 10:10|201209111625
SASI19|NAS38228|DEL37728|KARTHIKA|1952-10-29 00:00|201308071912
RADHA94|VAS37748|DEL37728|LALINKA|1952-10-29 00:00|201407061815
See a full description of this technique in How to filter logs easily with awk?请参阅如何使用 awk 轻松过滤日志中对该技术的完整描述? .
.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.