gpt4 book ai didi

json - jq substr() 相当于格式化一个值

转载 作者:行者123 更新时间:2023-12-04 00:01:15 26 4
gpt4 key购买 nike

希望你在这些时候一切都好。

我遇到了一个问题,希望你有更多的知识。

使用 'jq' 我想更改所有日期,例如。 19731013(字符串)到 1973-10-13

[
{
"Mail": "john@example.com",
"Name": "Smith",
"Employee_Number": "000555",
"First_Name": "John",
"Company": "ACME",
"Department": null,
"Employment_Status": "Retiree",
"Start_Date": "19770516",
"Function_Start_Date": "19770516",
"Group_Phone": "",
"Job_Title": "Operations Manager Warehousing",
"Sub_Group": "Exempts",
"Location": "Tibuktu",
"Organizational_Unit": null,
"Date_of_Birth": "19560719",
"Gender": "1"
},
{
"Mail": "mary@example.com",
"Name": "Smith",
"Employee_Number": "000777",
"First_Name": "Mary",
"Company": "ACME",
"Department": null,
"Employment_Status": "Retiree",
"Start_Date": "19770516",
"Function_Start_Date": "19770516",
"Group_Phone": "",
"Job_Title": "Manager",
"Sub_Group": "Exempts",
"Location": "Tibuktu",
"Organizational_Unit": null,
"Date_of_Birth": "19560719",
"Gender": "2"
}
]

是否可以使用 substr(.Start_Date,1,5)"-"substr(.Start_Date,6,2)"-"substr(.Start_Date,8,3)就像 CSV 中的 awk 一样?

也许我正盯着墙而错过了右边的门?

更新:感谢很多人,这就像一个魅力!
jq -r '.[].Start_Date |= "\(.[0:4])-\(.[4:6])-\(.[6:8])" | .[].Function_Start_Date |= "\(.[0:4])-\(.[4:6])-\(.[6:8])" | .[].Date_of_Birth|="\(.[0:4])-\(.[4:6])-\(.[6:8])"' employees.json > test.json

最佳答案

在 JQ 我们有 string slicestring interpolation语法。

$ jq '.[].Start_Date | "\(.[0:4])-\(.[4:6])-\(.[6:8])"' file
"1977-05-16"
"1977-05-16"

关于json - jq substr() 相当于格式化一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61012699/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com