linux - Change date format in a file with awk -
var1= date -d "19521029 1010" +"%y-%m-%d %h:%m" echo$var1
its working date -d
inside awk not working
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 convert fifth column of "file" user input date format. column number , date formats dynamic ie dt="%y-%m-%d %h:%m" , num=$5 depends on user requirement.
just use match()
catch data in 5th field , print back:
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 full description of technique in how filter logs awk?.
Comments
Post a Comment