Question: I want to use jq to check if a keyword exists as a key in a JSON at any level. Here is what I came up with:

```
jq -c 'paths | select(.[-1]) as $p| index("headline") //empty' news.json

```

The output is an array:

```
[
  6,
  7,
  9
]

```

I want to map the output array to jq function `any()` and get an overall result as true if key exists or false if it doesn't.

How can I get that done with jq ?

Answer: 
simple word
===========

If your word would be "easy", you can detect the occurrences count with:

```
echo "123 123 123" | sed "s/123 /123\n/g" | wc -l

```

In which the word is replace for the same but with `\n` and then `wc` count the lines

or you can try one of these:

* [Count occurrences of a char in a string using Bash](https://stackoverflow.com/questions/16679369/count-occurrences-of-a-char-in-a-string-using-bash)
* [How to count number of words from String using shell](https://stackoverflow.com/questions/15108229/how-to-count-number-of-words-from-string-using-shell)

complex
=======

Since your word is "complex" or you will want a pattern, you will need regex:

* <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions>
* <https://linuxconfig.org/advanced-bash-regex-with-examples>

**script.sh**

```
count=0

for word in $1; do
  if [[ "$word" =~ .*ge-0\.0\.3.* ]]
  then
    count=$(($count+1))
  fi
done

if [ "$count" -gt "1" ];
then
  echo "ge-0.0.3 is shown more than once"
else
  if [ "$count" -eq "0" ];
  then
    echo "ge-0.0.3 is not shown"
  else
    echo "ge-0.0.3 is shown once"
  fi
fi

```

**execution**

```
bash script.sh "srxa wan-a1 br-wan3-xa1 0A:AA:DD:C1:F1:A3 ge-0.0.3 srxa wan-a2 br-wan3-xa2 0A:AA:DD:C1:F2:A3 ge-0.0.3"

```

[![demo](https://i.stack.imgur.com/HL0FI.png)](https://i.stack.imgur.com/HL0FI.png)

grep
====

With grep you can get the ocurrence count

```
ocurrences=( $(grep -oE '(ge-0\.0\.3)' <<<$1) )
ocurrences_count=${#ocurrences[*]}
echo $ocurrences_count

```

[![enter image description here](https://i.stack.imgur.com/F5Z6W.png)](https://i.stack.imgur.com/F5Z6W.png)
Assuming you want to do a literal string match on whole words, not substrings, then this might be what you want:

```
$ if (( $(grep -oFw 'ge-0.0.3' <<<"$br_name" | wc -l) > 1 )); then echo 'yes'; else echo 'no'; fi
yes

```