jq with date and variable issue

onefastgoat

The below is running in bash as a script that is invoked manually at the moment.

I have been banging my head on this for the past few hours. I cannot seem for the life of me to pass a variable into jq no matter what way I do to it. When I set the variable manually without referencing my jq command and echo it, it works. However when it is placed inside my command it does not work.

I am thinking it has something to do with either the fromdateiso8601 or the < operator is causing an issue.

timestamp.json contains a few hundred of the below in the file. I need to reference every single one and pull out any that older than 45 days. Here is what I have tried.

date -d -45days +%s >> 45days.text
days=$(<45days.text)
echo $days    
cat $_fileformated | jq 'select(.Timestamp | . == null or fromdateiso8601? < "$days" ) .serverrname' >> $filereport
cat $_fileformated | jq -r --arg days "$days" 'select(.Timestamp | . == null or fromdateiso8601? < "$days" ) .serverrname' >> $filereport
cat $_fileformated | jq -r --arg days "$days" 'select(.Timestamp | . == null or fromdateiso8601? < env.days ) .serverrname' >> $filereport
cat $_fileformated | days="$days" jq 'select(.Timestamp | . == null or fromdateiso8601? < 'env.days' ) .serverrname' >> $filereport    
{
    "servername": "xxxxxxxx",
    "Timestamp": "2018-06-12T13:41:08Z"
}
peak

With your sample input and days=1524953907, the following query:

jq --argjson days $days '
  select(.Timestamp | select(fromdateiso8601 > $days)).servername'

yields:

"xxxxxxxx"

One can use --arg days $days but then you'd have to convert the string to a number, e.g. by $days|tonumber

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related