Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json jq substr()相当于格式化一个值_Json_String_Slice_Jq_Substr - Fatal编程技术网

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

Json jq substr()相当于格式化一个值,json,string,slice,jq,substr,Json,String,Slice,Jq,Substr,希望你在这段时间过得很好 我被一个问题难住了,我希望你有更多的知识 使用“jq”,我想将所有日期从例如19731013(字符串)更改为1973-10-13 [ { "Mail": "john@example.com", "Name": "Smith", "Employee_Number": "000555", "First_Name": "John", "Company": "ACME", "

希望你在这段时间过得很好

我被一个问题难住了,我希望你有更多的知识

使用“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"
    }
] 
是否可以像CSV中的awk一样使用
substr(.Start_Date,1,5)”-“substr(.Start_Date,6,2)”-“substr(.Start_Date,8,3)

也许我正盯着墙看,却错过了右边的门

更新:多谢各位,这真是太棒了

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中,我们有用于此的语法和语法

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

jq
中还有一个regex match函数,它使用
capture
发出命名的捕获组,这些组稍后可以通过
-
连接起来,形成所需的日期字符串

jq '.[].Start_Date | capture("(?<x>[0-9]{4})(?<y>[0-9]{2})(?<z>[0-9]{2})") | join("-")'
jq.[].Start|u Date | capture(([0-9]{4})([0-9]{2})([0-9]{2})| join(“-”)

这是假设您的
Start\u Date
字段长度至少为8个字符,并且不会验证小于该长度的字段。

谢谢您的支持。多亏了你,我今天学到了两件事。捕获()谢谢!我越来越喜欢jq了。一旦你了解了它的功能和语法,它就会非常强大。