
파일에 다음 텍스트가 있습니다
"rules": [
"/Common/Tetration_TCP_L4_ipfix",
"/Common/_sys_https_redirect"
],
"rulesReference": [
{
"link": "https://localhost/mgmt/tm/ltm/rule/~Common~Tetration_TCP_L4_ipfix?ver=12.0.0"
}
"rules": []
현재 내부의 내용을 추출하고 싶습니다 .
/Common/Tetration_TCP_L4_ipfix
/Common/_sys_https_redirect
출력은 다음과 같아야합니다
Tetration_TCP_L4_ipfix
_sys_https_redirect
하지만 이것도 무엇이든 될 수 있습니다.
답변1
입력 파일에 완전한 JSON이 포함되어 있지 않고 내부 조각(구조적으로는 유효하지만)만 포함되어 있는 경우 누락된 "외부" 부분을 복구하여 완전히 유효한 JSON 구조를 얻을 수 있습니다.
sed
+jq
해결책:
jq -r '.rules[] | sub("/Common/"; "")' <(sed '1 s/^/{/; $ s/$/]}/' file)
출력:
Tetration_TCP_L4_ipfix
_sys_https_redirect
물론, 실제로 유효한 JSON이 있다면 sed
처리를 건너뛰고 다음을 사용하세요.
jq -r '.rules[] | sub("/Common/"; "")' file
답변2
올바른 형식의 JSON 파일인 경우:
$ jq -r '..|select(type=="object" and has("rules")).rules|map(split("/")|.[-1])|.[]' file.json
Tetration_TCP_L4_ipfix
_sys_https_redirect
jq
이는 키 가 있는 모든 JSON 객체를 재귀적으로 찾는 데 사용됩니다 rules
. 해당 키의 모든 배열 값에 대해 값을 분할하고 /
해당 값의 마지막 구성 요소를 반환합니다.
답변3
사용AWK:
awk -F '[/"]' '/"rules": /,/],/{if(/"rules": |],/) next; print $(NF-1)}' file.txt