Reputation: 279
i'm trying to develop a shell script which scan a csv file (example below) and replace the EPOCH time stamp with the a readable time stamp.
the csv file looks like (column is EPOCH) :
1464007935578, 852111
1464007935600, 852111
1464007935603, 852111
1464007935603, 1900681
my script code:
#!/bin/bash
file=xxx.csv
while read line; do
epoch=`echo $line | awk -F',' '{print $1}'`
miliSec=$(($epoch % 1000 ))
timeWithoutMiliSec=`echo "$(date +"%T" -d@$(($epoch / 1000)))"`
fullTime=`echo "$timeWithoutMiliSec,$miliSec"`
echo $line | awk -F',' '{print $1}'|sed -i 's/[0-9]*/'$fullTime'/g' $file
done <$file
Desired output
12:52:15,255 852111
12:52:15,257 852111
12:52:15,259 852111
12:52:15,261 1900681
but when i run script it stuck and create many files start with sed* and i must kill the script in order to stop it. i think that the problem might be with the while loop combining with the sed while both (sed and while) take argument $file
can someone please advise about that issue?
Thanks a lot!
Upvotes: 0
Views: 167
Reputation: 289725
Use awk
's strftime()
function:
$ awk -F, -v OFS=, '{print strftime("%F", $1/1000), $2}' file
2016-05-23, 852111
2016-05-23, 852111
2016-05-23, 852111
2016-05-23, 1900681
This uses
%F
Equivalent to specifying ‘%Y-%m-%d’. This is the ISO 8601 date format.
Note I have to divide by 1000 because you have the miliseconds in your input.
Upvotes: 2