Hi
Please anyone to help to write script to convert date to 'yyyy-mm-dd'(If column is date convert) because my file has large and lot of date colums.
If file is small ,using awk command to achive.
cat file1|awk 'BEGIN {FS=OFS='|'} {$1=substr($1,1,4)"-"substr($1,5,2)"-"substr($1,7,2);{$2=substr($2,1,4)"-"substr($2,5,2)"-"substr($2,7,2);print $0}'
Thanks,
MR
Please provide an example of part of your file.
Hi,
Sample data
99990101|20080331|0.0000000|FBF7.375 12/09|0.0000000|20070809|20080908|0.0000000|12/09|.....
Thanks
MR
mohan705:
cat file1|awk 'BEGIN {FS=OFS='|'} {$1=substr($1,1,4)"-"substr($1,5,2)"-"substr($1,7,2);{$2=substr($2,1,4)"-"substr($2,5,2)"-"substr($2,7,2);print $0}'
This should give you a boost.
awk 'BEGIN {FS=OFS="|"} {for (i=1;i<=NF;i++) if (length($i)==8) $i=substr($i,1,4)"-"substr($i,5,2)"-"substr($i,7,2);print }' file1
Hi danmero,
Thanks ,This is what I am looking for.
Thanks,
MR
Hi danmero
How it can be handled If any othey colum that having length of 8 other than date columns ? (Is there any command to validate the date ).In that case it will take the columns data and convert to date format
Thanks,
MR
Yes, you can validate the date if you know the date range, here is a basic example:
awk 'BEGIN {FS=OFS="|"} {for (i=1;i<=NF;i++) if ($i ~ /[1-2][0-9][0-9][0-9][0-1][0-9][0-3][0-9]/) $i=substr($i,1,4)"-"substr($i,5,2)"-"substr($i,7,2);print }' file1
Hi danmero,
Thanks for your prompt reply
Thanks ,
MR