-1
var1= date -d "19521029 1010" +"%Y-%m-%d %H:%M"  

echo$var1

its working properly but date -d inside awk is 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 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.

Ben
  • 15
  • 7
  • 1
    Your `var1= date -d "..."` command is wrong. You need to say `var=$(command)`. Also, you cannot run external tools from awk the way you do: use `system()` or something similar. Finally, what is the expected output? – fedorqui Apr 19 '16 at 07:01

1 Answers1

1

Just use match() to catch the data in the 5th field and print it 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 a full description of this technique in How to filter logs easily with awk?.

Community
  • 1
  • 1
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • 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 i got wrong output at the place of 5th column 0-0-0 : – Ben Apr 19 '16 at 12:05
  • @Ben my answer works fine with the file you initially provided. Do give a [mcve] so that we can work with it. Otherwise, this will lead to frustration in both ends: you not getting anything, we trying to solve something that keeps changing. – fedorqui Apr 19 '16 at 12:37
  • ok sorry for that the file i provided is an example file its contents i provided 5th and 6th are date columns and user input date column and his desired date formats – Ben Apr 19 '16 at 12:53
  • @Ben you say in your question _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_ I cannot do much for that. You may have `match($line, ...)` and give `line` as a variable. Regarding the format, it would need some work that cannot be done without further details. – fedorqui Apr 19 '16 at 12:55
  • using my above awk command can you help me to solve the [awk -F '|' '{$5=date -d $5+"%Y-%m-%d %H:%M"; {OFS="|"}; print}' file] command or i try another one awk 'BEGIN {FS=OFS="|"}{$5=strftime("%c",$5)} {print}' file but it also give wrong answer – Ben Apr 19 '16 at 13:06