How to extract data after a field in a file

Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I have following text in a file
"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"
want to extract anything inside "rules":
currently it is
/Common/Tetration_TCP_L4_ipfix
/Common/_sys_https_redirect
Output should look like
Tetration_TCP_L4_ipfix
_sys_https_redirect
but again this can be anything.
text-processing awk
add a comment |Â
up vote
0
down vote
favorite
I have following text in a file
"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"
want to extract anything inside "rules":
currently it is
/Common/Tetration_TCP_L4_ipfix
/Common/_sys_https_redirect
Output should look like
Tetration_TCP_L4_ipfix
_sys_https_redirect
but again this can be anything.
text-processing awk
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have following text in a file
"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"
want to extract anything inside "rules":
currently it is
/Common/Tetration_TCP_L4_ipfix
/Common/_sys_https_redirect
Output should look like
Tetration_TCP_L4_ipfix
_sys_https_redirect
but again this can be anything.
text-processing awk
I have following text in a file
"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"
want to extract anything inside "rules":
currently it is
/Common/Tetration_TCP_L4_ipfix
/Common/_sys_https_redirect
Output should look like
Tetration_TCP_L4_ipfix
_sys_https_redirect
but again this can be anything.
text-processing awk
edited May 30 at 7:40
Kusalananda
102k13199313
102k13199313
asked May 30 at 6:13
Sanjay Shitole
6
6
add a comment |Â
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
4
down vote
If your input file contains non full-fledged JSON but only inner fragment (though structurally valid) you may recover the missing "outer" parts to get a full valid JSON structure.
sed + jq solution:
jq -r '.rules | sub("/Common/"; "")' <(sed '1 s/^//; $ s/$/]/' file)
The output:
Tetration_TCP_L4_ipfix
_sys_https_redirect
Of course, if in real you have a valid JSON - just skip the sed processing and just use:
jq -r '.rules | sub("/Common/"; "")' file
add a comment |Â
up vote
3
down vote
If this is a well-formed JSON file:
$ jq -r '..|select(type=="object" and has("rules")).rules|map(split("/")|.[-1])|.' file.json
Tetration_TCP_L4_ipfix
_sys_https_redirect
This will use jq to recursively find all the JSON objects that has a rules key. For all the array values of those keys, it will split the value on / and return the last component of it.
add a comment |Â
up vote
3
down vote
Using AWK:
awk -F '[/"]' '/"rules": /,/],/if(/"rules": ' file.txt
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
If your input file contains non full-fledged JSON but only inner fragment (though structurally valid) you may recover the missing "outer" parts to get a full valid JSON structure.
sed + jq solution:
jq -r '.rules | sub("/Common/"; "")' <(sed '1 s/^//; $ s/$/]/' file)
The output:
Tetration_TCP_L4_ipfix
_sys_https_redirect
Of course, if in real you have a valid JSON - just skip the sed processing and just use:
jq -r '.rules | sub("/Common/"; "")' file
add a comment |Â
up vote
4
down vote
If your input file contains non full-fledged JSON but only inner fragment (though structurally valid) you may recover the missing "outer" parts to get a full valid JSON structure.
sed + jq solution:
jq -r '.rules | sub("/Common/"; "")' <(sed '1 s/^//; $ s/$/]/' file)
The output:
Tetration_TCP_L4_ipfix
_sys_https_redirect
Of course, if in real you have a valid JSON - just skip the sed processing and just use:
jq -r '.rules | sub("/Common/"; "")' file
add a comment |Â
up vote
4
down vote
up vote
4
down vote
If your input file contains non full-fledged JSON but only inner fragment (though structurally valid) you may recover the missing "outer" parts to get a full valid JSON structure.
sed + jq solution:
jq -r '.rules | sub("/Common/"; "")' <(sed '1 s/^//; $ s/$/]/' file)
The output:
Tetration_TCP_L4_ipfix
_sys_https_redirect
Of course, if in real you have a valid JSON - just skip the sed processing and just use:
jq -r '.rules | sub("/Common/"; "")' file
If your input file contains non full-fledged JSON but only inner fragment (though structurally valid) you may recover the missing "outer" parts to get a full valid JSON structure.
sed + jq solution:
jq -r '.rules | sub("/Common/"; "")' <(sed '1 s/^//; $ s/$/]/' file)
The output:
Tetration_TCP_L4_ipfix
_sys_https_redirect
Of course, if in real you have a valid JSON - just skip the sed processing and just use:
jq -r '.rules | sub("/Common/"; "")' file
answered May 30 at 7:31
RomanPerekhrest
22.4k12144
22.4k12144
add a comment |Â
add a comment |Â
up vote
3
down vote
If this is a well-formed JSON file:
$ jq -r '..|select(type=="object" and has("rules")).rules|map(split("/")|.[-1])|.' file.json
Tetration_TCP_L4_ipfix
_sys_https_redirect
This will use jq to recursively find all the JSON objects that has a rules key. For all the array values of those keys, it will split the value on / and return the last component of it.
add a comment |Â
up vote
3
down vote
If this is a well-formed JSON file:
$ jq -r '..|select(type=="object" and has("rules")).rules|map(split("/")|.[-1])|.' file.json
Tetration_TCP_L4_ipfix
_sys_https_redirect
This will use jq to recursively find all the JSON objects that has a rules key. For all the array values of those keys, it will split the value on / and return the last component of it.
add a comment |Â
up vote
3
down vote
up vote
3
down vote
If this is a well-formed JSON file:
$ jq -r '..|select(type=="object" and has("rules")).rules|map(split("/")|.[-1])|.' file.json
Tetration_TCP_L4_ipfix
_sys_https_redirect
This will use jq to recursively find all the JSON objects that has a rules key. For all the array values of those keys, it will split the value on / and return the last component of it.
If this is a well-formed JSON file:
$ jq -r '..|select(type=="object" and has("rules")).rules|map(split("/")|.[-1])|.' file.json
Tetration_TCP_L4_ipfix
_sys_https_redirect
This will use jq to recursively find all the JSON objects that has a rules key. For all the array values of those keys, it will split the value on / and return the last component of it.
answered May 30 at 6:31
Kusalananda
102k13199313
102k13199313
add a comment |Â
add a comment |Â
up vote
3
down vote
Using AWK:
awk -F '[/"]' '/"rules": /,/],/if(/"rules": ' file.txt
add a comment |Â
up vote
3
down vote
Using AWK:
awk -F '[/"]' '/"rules": /,/],/if(/"rules": ' file.txt
add a comment |Â
up vote
3
down vote
up vote
3
down vote
Using AWK:
awk -F '[/"]' '/"rules": /,/],/if(/"rules": ' file.txt
Using AWK:
awk -F '[/"]' '/"rules": /,/],/if(/"rules": ' file.txt
edited May 30 at 7:15
answered May 30 at 6:34
SivaPrasath
4,54212243
4,54212243
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f446832%2fhow-to-extract-data-after-a-field-in-a-file%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password