在Json下面,我同时具有有效和无效的文本。我想从中提取有效的字符串。 有效的Json字符串以{“” emp_id“”开始,并以“” unit_id“”:“” true“”}}
结尾起始字符串是固定的,但是在字符串““ unit_id””的结尾之后,我们可能会再次获得多个键值对 像“” unit_id“”:“” true“”,“” ind_val“”:“ 1”,“” active_flg“”:“” Y“”}}等,但是它总是以两个大括号}} >
.915-0700",,,"42,795""34434",.915-0700",,,"42,795""34434",{""emp_id"":""212345"",""emp_request"":{""request_header"":{""dept_id"":""20182166008974"",""client_id"":""AP"",""medium_id"":""Web"",""country"":""US"",""request_time"":""10:59:42.719""},""mp_req"":{""contexts"":[""PO""],""user_id"":{""ref_id"":"""",""date_of_birth"":""2012-12-12"",""emp_objects"":[{""emp_number"":null,""emp_number_enc"":null,""emp_n1"":""18"",""emp_n2_enc"":null,""emp_n3"":null,""dept_enc"":null}],""char_enc"":null,""ttp"":""304""},""req_info"":{""dept_code"":""H2"",""address"":null,""grp_code"":""S000043K""}}},""additional_request"":{""tax"":"""",""unit_id"":""true""}}^""","2018-08-0607:59:42.915-0700",,,"42,795""34434","mgr_hir":"yes","34545",,,,,
此命令给出以下输出
grep -o '{""emp_id[^}]*}' file.json
{""emp_id"":""212345"",""emp_request"":{""request_header"":{""dept_id"":""20182166008974"",""client_id"":""AP"",""medium_id"":""Web"",""country"":""US"",""request_time"":""10:59:42.719""}
我试图在“” unit_id“”之后指定结束模式:它可以有多个字符/键值对,直到到达}}为止,但是由于下面的命令
中缺少某些内容而无法正常工作grep -o '{""emp_id[^}]*""unit_id"":[a-z]}}$' file.json
请提出建议。