I have a file with specific dates and i want to convert them to UTC format. so I prepared a small script
I get below errors:
date: option requires an argument -- 'd'
Try `date --help' for more information.
date: extra operand `16:16:53'
Content of my File looks this:
20191014161653042nmd
20191014161653052egc
20191004081901490egc
20191004081901493nex
20191004081901497nex
20191004081902531nex
20191004081902534ksd
My code looks like this:
for i in $(cut -f1 Firstfile)
do
echo "$i" > tmpfile
TIME=$(awk '{print substr($1,1,14)}' tmpfile | sed -re 's/^([0-9]{8})([0-9]{2})([0-9]{2})([0-9]{2})$/\1\\ \2:\3:\4/' | xargs date +@%s -d | xargs date -u +"%Y-%m-%dT%H:%M:%S" -d)
MSec=$(awk '{print substr($1,15,3)}' tmpfile)
Msg=$(awk '{print substr($1,18,3)}' tmpfile)
echo -e "$TIME.$MSec $Msg" >> ResultFile
done
When I use the command individually it works fine and I get the desired result.
awk '{print substr($1,1,14)}' tmpfile | sed -re 's/^([0-9]{8})([0-9]{2})([0-9]{2})([0-9]{2})$/\1\\ \2:\3:\4/' | xargs date +@%s -d | xargs date -u +"%Y-%m-%dT%H:%M:%S" -d
What is the mistake I am doing in this with the script? why does it not work when I pass it through a script in for loop?
Expected Result:
2019-10-14T20:16:52.042 nmd
2019-10-14T20:16:52.052 egc
2019-10-04T12:19:01.490 egc
and so on
gawk
)? if so you can probably simplify the conversion significantly using the builtinmktime
andstrftime
xargs -n1
to pass one argument after another todate
. A more compact version would besed -re 's/^([0-9]{8})([0-9]{2})([0-9]{2})([0-9]{2}).*/\1\\ \2:\3:\4/' Firstfile | xargs -n1 date +@%s -d | xargs -n1 date -u +"%FT%T" -d >> ResultFile