2021年4月26日 星期一

How to calculate days between two dates using bash 怎麼計算兩個日期中間有幾天

 How to calculate days between two dates using bash 怎麼計算兩個日期中間有幾天

 echo $((($(date -d "2021-04-26" +%s)-$(date -d "2021-01-01" +%s))/86400))

115

 echo $((($(date +%s -d 20210426)-$(date +%s -d 20210101))/86000))

115

2021年4月14日 星期三

透過 jq 處理 json data 如同 sed 預處理資料 以elasitcsearch aggregation 爲例

透過 jq 處理 json data 如同 sed 預處理資料  以elasitcsearch aggregation 爲例


如何在command line中,快速處理json response, 可要使用 jq 來處理洗清整理轉換成我們所需要的資料結構,如果 sed awk grep...等工具

  jq https://stedolan.github.io/jq/

"jq is like sed for JSON data - you can use it to slice and filter and map and transform structured data with the same ease that sed, awk, grep and friends let you play with text."

這裏以 elasitcsearch aggregation 爲例 來看看 jq 怎麼處理 json 成我們需要的格式

使用 elasitcsearch aggregation api 得到商品資料庫中,各個商家的前三多的分類統計。 


GET products/_search
{"size":0,"aggs":{"top_store":{"terms":{"field":"merchant","size":1},"aggs":{"cate":{"terms":{"field":"category","size":3}}}}}}
{
  "took" : 1540,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 10000,
      "relation" : "gte"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "top_store" : {
      "doc_count_error_upper_bound" : 135988,
      "sum_other_doc_count" : 26449882,
      "buckets" : [
        {
          "key" : "Amazon",
          "doc_count" : 9212673,
          "cate" : {
            "doc_count_error_upper_bound" : 18623,
            "sum_other_doc_count" : 723560,
            "buckets" : [
              {
                "key" : 79,
                "doc_count" : 585058
              },
              {
                "key" : 67,
                "doc_count" : 355320
              },
              {
                "key" : 30,
                "doc_count" : 115691
              }
            ]
          }
        }
      ]
    }
  }
}

使用的 jq 的 syntax 如下,

 cat res.json | jq ' .aggregations.top_store.buckets[] | [.key,.cate.buckets[].key,.cate.buckets[].doc_count] | @tsv' -r

Amazon 79 67 30 585058 355320 115691


如此我們可以使用 jq 整理洗清原始json,得到需要格式的資料。
stedolan/jq: Command-line JSON processor https://github.com/stedolan/jq
Terms aggregation | Elasticsearch Guide [7.12] | Elastic https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html


2021年4月13日 星期二

vscode Sent env to running instance. Terminating...

 vscode Sent env to running instance. Terminating...

$ code --verbose

[main 09:16:06] Sending env to running instance...

[main 09:16:06] Sent env to running instance. Terminating...

ps aux | grep 'Visual Studio Code' | awk '{print $2}' | xargs kill -9