Skip to content

Commit c44e697

Browse files
authored
Merge pull request HarshCasper#416 from hardword/master
json file parsing with jq in bash
2 parents b61c169 + 055144f commit c44e697

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

Bash/JSON_Parse_with_jq/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
### json parsing in bash
2+
3+
`find_flag.sh` sctipt will tell you how to use [jq](https://stedolan.github.io/jq/) command to parse JSON file in bash with an example (`sample.json`)
4+
5+
The script will run below 3 commands and find the flag from the `sample.json` file
6+
7+
```bash
8+
$ cat sample.json | jq "." | head -n 13
9+
```
10+
11+
```bash
12+
$ cat sample.json | jq ".data.allPosts.edges[0:3]"
13+
```
14+
15+
```bash
16+
$ cat sample.json | jq ".data.allPosts.edges" | grep -E -o 'RotteN{.*}' | grep -v 'harder'
17+
```

Bash/JSON_Parse_with_jq/find_flag.sh

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/bin/bash
2+
3+
echo "##########################################################"
4+
echo "### Let's check first few lines of the json file ..."
5+
echo "##########################################################"
6+
echo ""
7+
echo "$ cat sample.json | jq \".\" | head -n 13"
8+
echo ""
9+
echo "Press any key to continue"
10+
while [ true ] ; do
11+
read -t 3 -n 1
12+
if [ $? = 0 ] ; then
13+
cat sample.json | jq "." | head -n 13
14+
break
15+
else
16+
echo "..."
17+
fi
18+
done
19+
20+
echo ""
21+
echo "##########################################################"
22+
echo "### Now analyzing a couple of node objects under edges... "
23+
echo "##########################################################"
24+
echo ""
25+
echo "$ cat sample.json | jq \".data.allPosts.edges[0:3]\""
26+
echo ""
27+
echo "Press any key to continue"
28+
while [ true ] ; do
29+
read -t 3 -n 1
30+
if [ $? = 0 ] ; then
31+
cat sample.json | jq ".data.allPosts.edges[0:3]"
32+
break
33+
else
34+
echo "..."
35+
fi
36+
done
37+
38+
echo ""
39+
echo "##########################################################"
40+
echo "### We'll use grep to get a unique secret value..."
41+
echo "##########################################################"
42+
echo ""
43+
echo "$ cat sample.json | jq \".data.allPosts.edges\" | grep -E -o 'RotteN{.*}' | grep -v 'harder'"
44+
echo ""
45+
echo "Press any key to continue"
46+
while [ true ] ; do
47+
read -t 3 -n 1
48+
if [ $? = 0 ] ; then
49+
cat sample.json | jq ".data.allPosts.edges" | grep -E -o "RotteN{.*}" | grep -v "harder"
50+
exit
51+
else
52+
echo "..."
53+
fi
54+
done

Bash/JSON_Parse_with_jq/sample.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)