Search for string in json files: Difference between revisions
Jump to navigation
Jump to search
(Created page with "Search for string in multiple JSON files == Approaches == === Approach 1: Content of JSON file contains the keyword(s) === * Input: (1) Specify the folder path (2) Specify th...") |
|||
| Line 13: | Line 13: | ||
* Output: file name and the value of specified index/column | * Output: file name and the value of specified index/column | ||
* Command 1: {{kbd | key=<nowiki>find . -name '*.json' | xargs -I {} jq '{"file": input_filename, "MYCOLUMN": ..|.MYCOLUMN?}' {} | jq -c 'select(.MYCOLUMN | contains("詐騙"))'</nowiki>}} to find json files which content of column {{kbd | key=MYCOLUMN}} contains the string {{kbd | key=<nowiki>詐騙</nowiki>}}<ref>[https://medium.com/@marvinirwin/bash-snippet-find-every-file-containing-a-non-empty-array-value-with-find-xargs-and-jq-cc456d3705e4 bash snippet: Find every file containing a non empty array value with find, xargs, and jq | by Marvin Irwin | Medium]</ref>. | * Command 1: {{kbd | key=<nowiki>find . -name '*.json' | xargs -I {} jq '{"file": input_filename, "MYCOLUMN": ..|.MYCOLUMN?}' {} | jq -c 'select(.MYCOLUMN | contains("詐騙"))'</nowiki>}} to find json files which content of column {{kbd | key=MYCOLUMN}} contains the string {{kbd | key=<nowiki>詐騙</nowiki>}}<ref>[https://medium.com/@marvinirwin/bash-snippet-find-every-file-containing-a-non-empty-array-value-with-find-xargs-and-jq-cc456d3705e4 bash snippet: Find every file containing a non empty array value with find, xargs, and jq | by Marvin Irwin | Medium]</ref>. | ||
* Command 2: {{kbd | key=<nowiki>find . -name '*.json' | xargs -I {} jq '{"file": input_filename, "MYCOLUMN": ..|.MYCOLUMN?}' {} | jq -c 'select(.MYCOLUMN | contains("詐騙") or contains("詐欺"))'</nowiki>}} to find json files which content of column {{kbd | key=MYCOLUMN}} contains the string {{kbd | key=<nowiki>詐騙</nowiki>}} or {{kbd | key=<nowiki>詐欺</nowiki>}}. | * Command 2 for multiple keywords using boolean OR operator: {{kbd | key=<nowiki>find . -name '*.json' | xargs -I {} jq '{"file": input_filename, "MYCOLUMN": ..|.MYCOLUMN?}' {} | jq -c 'select(.MYCOLUMN | contains("詐騙") or contains("詐欺"))'</nowiki>}} to find json files which content of column {{kbd | key=MYCOLUMN}} contains the string {{kbd | key=<nowiki>詐騙</nowiki>}} or {{kbd | key=<nowiki>詐欺</nowiki>}}. | ||
* Command 3 for multiple keywords using boolean AND operator: {{kbd | key=<nowiki>find . -name '*.json' | xargs -I {} jq '{"file": input_filename, "MYCOLUMN": ..|.MYCOLUMN?}' {} | jq -c 'select(.MYCOLUMN | contains("詐騙") and contains("詐欺"))'</nowiki>}} to find json files which content of column {{kbd | key=MYCOLUMN}} contains the string {{kbd | key=<nowiki>詐騙</nowiki>}} and {{kbd | key=<nowiki>詐欺</nowiki>}}. | |||
== References == | == References == | ||
Latest revision as of 19:38, 9 March 2021
Search for string in multiple JSON files
Approaches[edit]
Approach 1: Content of JSON file contains the keyword(s)[edit]
- Input: (1) Specify the folder path (2) Specify the keyword(s)
- Output: file name list
- Command: find *.json | xargs grep -E "詐騙|詐欺" -sl to find json files which its content contains the string 詐騙 or 詐欺.
Approach 2: The specified index/column contains the keyword(s)[edit]
- Requirement: Install jq
- Input: (1) Specify the folder path (2) Specify the keyword(s) (3) Specify the index/column of JSON
- Output: file name and the value of specified index/column
- Command 1: find . -name '*.json' | xargs -I {} jq '{"file": input_filename, "MYCOLUMN": ..|.MYCOLUMN?}' {} | jq -c 'select(.MYCOLUMN | contains("詐騙"))' to find json files which content of column MYCOLUMN contains the string 詐騙[1].
- Command 2 for multiple keywords using boolean OR operator: find . -name '*.json' | xargs -I {} jq '{"file": input_filename, "MYCOLUMN": ..|.MYCOLUMN?}' {} | jq -c 'select(.MYCOLUMN | contains("詐騙") or contains("詐欺"))' to find json files which content of column MYCOLUMN contains the string 詐騙 or 詐欺.
- Command 3 for multiple keywords using boolean AND operator: find . -name '*.json' | xargs -I {} jq '{"file": input_filename, "MYCOLUMN": ..|.MYCOLUMN?}' {} | jq -c 'select(.MYCOLUMN | contains("詐騙") and contains("詐欺"))' to find json files which content of column MYCOLUMN contains the string 詐騙 and 詐欺.