简体   繁体   中英

time format change in shell

In my log file the time format is 2014-10-10 HH:MM:SS:sss . I am reading the time and date from the file and converting it into seconds for further processing. But it's giving error the date is invalid. May be it's because time is not in ..SS.sss format.

while read line;
do
 d1=$(echo $line | cut -d, -f2);
case "$line" in \s*) continue ;; esac
t1=$(echo $line | cut -d, -f3);
d1t1="${d1} ${t1}";
echo "$d1t1";
ds1=$(date -d"$d1t1" "+%s");
echo "$ds1";
done < error.log

I want to replace ":" by "." which is the way to solve the problem?

Why not to simply replace the milliseconds separator in your log file?

perl -p -i -e 's/:(\d{3}),/.\1/g' error.log

A ':' separator for milliseconds is wrong , and date command will complain aboult it. With a '.' separator date command will succeed.

UPDATE:
The command is a "perl inliner": it applies text changes to a file ("error.log", in this case), globally (for every row).
The change command here is a regular expression substitution: s/:(\\d{3}),/.\\1/g , which means: " substitute every colon followed by exaclty 3 digits and a comma with a dot and the digits ".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM