I have a file which contains timestamp and date in the second column. If the line contains one of the word then it need to be replace like below. Any help is appreciated.
File:
a smallint
b timestamp
c date
d varchar
O/P:
a smallint
dateformat(b,'YYYY-MM-DD HH:NN:SS.sss')
dateformat(c.'YYYY-MM-DD')
d varchar
------ Post updated at 06:21 PM ------
awk '{if ($2=="dateformat') {$1=dataformat($1,'YYYY-MM-DD HH:NN:SS.sss')} else {$2=1}}
------ Post updated at 06:27 PM ------
awk '{if ($2=="dateformat") {$1=dataformat($1,'YYYY-MM-DD HH:NN:SS.sss')} else {$2=1}}' test.out
------ Post updated at 06:34 PM ------
sorry, I have entered dateformat instead of timestamp but still its not working
awk '{if ($2=="timestamp") {$1="dataformat($1,'YYYY-MM-DD')"} else {$2=1}}' test.out
------ Post updated at 07:40 PM ------
If I execute the statement in a separate awk
statement then its working fine but If I add in the else condition then I am getting error.
awk '{if ($2=="timestamp") {$3="dataformat("; }; print $3 $1 ",'\''YYYY-MM-DD HH'\:'NN'\:'SS'\.'sss)" else ($2=="date") {$3="dataformat("; }; print $3 $1 ",'\''YYYY-MM-DD)" }' test.out
Error:
awk: {if ($2=="timestamp") {$3="dataformat("; }; print $3 $1 ",'YYYY-MM-DD HH:NN:SS.sss)" else ($2=="date") {$3="dataformat("; }; print $3 $1 ",'YYYY-MM-DD)" }
awk: ^ syntax error
awk: {if ($2=="timestamp") {$3="dataformat("; }; print $3 $1 ",'YYYY-MM-DD HH:NN:SS.sss)" else ($2=="date") {$3="dataformat("; }; print $3 $1 ",'YYYY-MM-DD)" }
awk: ^ syntax error
------ Post updated at 08:45 PM ------
awk '{if ($2=="timestamp") {$3="dataformat("; }; print $3 $1 ",'\''YYYY-MM-DD HH'\:'NN'\:'SS'\.'sss)" else ($2=="date") {$3="dataformat("; }; print $3 $1 ",'\''YYYY-MM-DD)" }' test.out