I have a json file from where I want to get the value of two items which are in an array using grep. Let's say I'm trying to get data from values foo and bar, the problem is that baralso matches megabarso I have to grep it with option -w.
The problem comes when trying to find them at the same time, as I have to grep for one value OR the other.
From this question, I managed to get a bit closer to what I'm looking for but my result was something like:

grep -E '(foo|-w bar)' file
 "foo": "59766",
 "foo": "59770",
 "foo": "59774",
 "foo": "59778",
 "foo": "59782",
 "foo": "59786",

Any idea on how to do it?

  • Does it need to be one grep call under all circumstances?
    – phk
    May 27, 2016 at 12:00
  • Yes, I need to have them related to each others. I'm trying to get foo and in the next line bar so I can work with them later
    – sysfiend
    May 27, 2016 at 12:01
  • 6
    if you're parsing json, you probably should be using jq or jsonpipe or use a language (like perl or python) which has good json-parsing libraries.
    – cas
    May 27, 2016 at 14:56

2 Answers 2


The word boundary has a similar effect to -w, but can be used as part of the expression.

Match the empty string at the edge of a word.
Match the empty string at the beginning of word.
Match the empty string at the end of word.

To match bar only when it's the whole word, but foo anywhere (including inside longer words):

grep -E 'foo|\<bar\>'

Given that foo would be used in the same context as foo and also match a full word, the easiest way is to do:

grep -E -w '(foo|bar)' file
  • As already answered 11 hours before, that's not what I'm looking for
    – sysfiend
    May 28, 2016 at 1:26
  • @Alex since you did seem to want foo not to match a full word, I looked for it several times, but since the question didn't specify it, seemed appropriate to note the easier answer nevertheless.
    – Ángel
    May 29, 2016 at 21:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .