Reputation: 11691
I have an array that is being stored in a bash variable that is a json array of dates (it's currently being produced by a separate script). It looks like the following
["2019-09-19 03:13:29", "2019-09-19 20:20:18", "2019-09-19 18:19:50", "2019-09-19 06:07:17", "2019-09-19 11:53:25"]
I want to iterate over these dates and preserve the white space in the dates so I can use them with the gdate
command. I'm currently using jq
but that is splitting up the date part from the time part. i.e.
dates=$(python date_producing_script.py | jq -c -r '.[]')
for date in ${dates[@]}; do
echo $date
#end goal is to do something here w/ gdate ex gdate -d $date ...
done
However this gives something like
2019-09-19
03:13:29
2019-09-19
20:20:18
2019-09-19
18:19:50
2019-09-19
06:07:17
2019-09-19
11:53:25
Where I'm looking for something like the following
2019-09-19 03:13:29
2019-09-19 20:20:18
2019-09-19 18:19:50
2019-09-19 06:07:17
2019-09-19 11:53:25
(one confusing thing for me here is that if I just do the python date_producing_script.py | jq -c -r '.[]'
command in a terminal it looks the way I want it to)
However ideally I would want something like. Is there anyway to get this result from the input in shell script
Upvotes: 1
Views: 2870
Reputation: 1692
One of your issue is when you populate dates
array ,
and the other is when you want to use the array for loop .
My proposal is :
=
as a separator =
as separator instead of <space><tab><line return>
"$dates[@]"
instead of $dates[@]
Your script will be like :
OLDIFS=$IFS ;
IFS="=" ;
dates=( python date_producing_script.py | jq -c -j -r '.[]|(.,"=")') );
IFS=$OLDIFS
for d in "${dates[@]}" ;
do
echo $d ;
done
Upvotes: 0
Reputation: 116770
Your jq is fine; the problem is in the way you're looping through the bash array, as can be seen from this bash typescript:
$ ary=("a b" "c d")
$ for x in ${ary[@]} ; do echo "$x" ; done
a
b
c
d
$ for x in "${ary[@]}" ; do echo "$x" ; done
a b
c d
$
"${dates[@]}"
Upvotes: 4
Reputation: 8611
Here is one solution:
#!/bin/bash
#
IFS=','
dates='["2019-09-19 03:13:29", "2019-09-19 20:20:18", "2019-09-19 18:19:50", "2019-09-19 06:07:17", "2019-09-19 11:53:25"]'
for date in ${dates[@]}
do
# Cleanup $date
thedate=$(echo $date | tr -d '\[\]"' | sed -e 's/^[[:space:]]*//')
echo "-->$thedate<--"
done
$dates
is now separated on ,$thedate
is $date
with [
, ]
, "
removed. The sed
is to remove prefix spaces.-->
and <--
to show that $thedate
does not include any spaces.There are most probably solutions with awk
too.
Upvotes: 0