joy joy - 22 days ago 5
Bash Question

Apply set of operation on one entire coloumn in unix file

I have a file looks like :

"JOB1 #43", 43 "SUCCESS", 1479079800029
"JOB1 #42", 42 "SUCCESS", 1478993400042
"JOB1 #41", 41 "SUCCESS", 1478907000065
"JOB1 #40", 40 "SUCCESS", 1478820600085
"JOB1 #39", 39 "SUCCESS", 1478734200051


need to change last column (timestamp) with date and time format.

tried so far :

for i in `cat file | awk '{print $NF}'`
do
date -d @$( echo "(${i} + 500) / 1000" | bc)
done


oputput:

Sun Nov 13 23:30:00 GMT 2016
Sat Nov 12 23:30:00 GMT 2016
Fri Nov 11 23:30:00 GMT 2016
Thu Nov 10 23:30:00 GMT 2016
Wed Nov 9 23:30:00 GMT 2016


expected output is

"JOB1 #43", 43 "SUCCESS", Sun Nov 13 23:30:00 GMT 2016
"JOB1 #42", 42 "SUCCESS", Sat Nov 12 23:30:00 GMT 2016
"JOB1 #41", 41 "SUCCESS", Fri Nov 11 23:30:00 GMT 2016
"JOB1 #40", 40 "SUCCESS", Thu Nov 10 23:30:00 GMT 2016
"JOB1 #39", 39 "SUCCESS", Wed Nov 9 23:30:00 GMT 2016


can we change it in file itself?
any help would be appriciated.

Answer

Try:

awk -F',\\s*' '{print $1 "," $2 "," strftime("%c", $3/1000)}' file

Outputs:

"JOB1 #43",43 "SUCCESS",Sun 13 Nov 2016 23:30:00 GMT
"JOB1 #42",42 "SUCCESS",Sat 12 Nov 2016 23:30:00 GMT
"JOB1 #41",41 "SUCCESS",Fri 11 Nov 2016 23:30:00 GMT
"JOB1 #40",40 "SUCCESS",Thu 10 Nov 2016 23:30:00 GMT
"JOB1 #39",39 "SUCCESS",Wed 09 Nov 2016 23:30:00 GMT

You may like to adjust the date format as necessary.