Created
March 31, 2020 11:37
-
-
Save kawahara-kunio/3d79f72d9473f7c1f16ce670758562e1 to your computer and use it in GitHub Desktop.
filter script to convert Unix timestamp millisecond format into date format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# 使い方 | |
# cat xxx.json | jq -M . | timestamp2date_filter [| jq .] | |
# (jq のカラー出力には対応していないので -M でカラー無し出力後にフィルタする | |
IFS=$'\n' | |
TIMESTAMP_REGEXP=".*timestamp_?.*\":[[:blank:]]*([0-9]+)([0-9]{3})" | |
OS=$(uname) | |
if [ ${OS} != 'Darwin' ]; then | |
OS="Linux" | |
fi | |
convert2date () { | |
if [ ${OS} == "Darwin" ]; then | |
TZ=Asia/Tokyo date -r $1 "+%Y-%m-%dT%H:%M:%S.$2 %z" | |
else | |
TZ=Asia/Tokyo date --date=@$1 "+%Y-%m-%dT%H:%M:%S.$2 %z" | |
fi | |
} | |
while read -r line | |
do | |
if [[ ${line} =~ ${TIMESTAMP_REGEXP} ]]; then | |
timestamp=${BASH_REMATCH[1]} | |
millisecond=${BASH_REMATCH[2]} | |
date_value=$(convert2date ${timestamp} ${millisecond}) | |
replaced_line=$(echo ${line} | sed -Ee "s/${timestamp}${millisecond}/\"${date_value}\"/") | |
echo ${replaced_line} | |
else | |
echo ${line} | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment