I am trying to find all instances of "type":"FollowEvent", and then within those instances, if the string "actor": is not followed by {, then capture the string enclosed in " that comes immediately after "actor":. Else, capture the string enclosed in " that comes immediately after "login:".

What I have so far:

zgrep -e '"type":"FollowEvent"' /path/to/dir/* | zgrep -o '"actor":(?!{)*' | cut -f2- -d: | cut -d',' -f1 > results_file.txt

EDIT: What this zgrep does:

For all files in /path/to/dir, for all lines that contain "type":"FollowEvent", find "actor:" not followed by {. Then take everything after the :, and before the next ,. Put the results in results_file.txt.


A single line in the files that are being grep'd could look like this:


or like this:

  • 3
    As this looks like JSON format why do not use jq? – Romeo Ninov Jun 4 at 18:19
  • Why the downvote? Also, if jq is a solution, could you please show me? – StatsSorceress Jun 4 at 18:34
  • 1
    Post a sample JSON snippet containing the instance and show us how would you like to parse it – Inian Jun 4 at 18:34
  • 1
    I find it difficult to follow your greps. What is the actual data that you'd like to extract from this JSON document? Is it the target.login bit? – Kusalananda Jun 4 at 18:38
  • Thank you @Inian, I've edited to explain further. – StatsSorceress Jun 4 at 20:11

The username listed as login under target can be had from both JSON documents using jq:

$ jq -r '.payload.target.login' file1.json

Likewise, the login under actor can be had:

$ jq -r '.actor.login' file1.json

If you want to make the output conditional on the type (and not produce anything if the type is not FollowEvent):

jq -r 'select(.type == "FollowEvent") | .actor.login' file1.json
  • I cannot use jq, because I cannot install it. I'm working with Python 2.7, which I suspect is part of the issue. – StatsSorceress Jun 4 at 19:56
  • 1
    @StatsSorceress jq has no dependency on Python. You could easily use a JSON parser in Python if you wished to do that instead. – Kusalananda Jun 4 at 20:59
  • Thanks @Kusalananda, but I can't install jq. Is there a way to do if/else within grep? – StatsSorceress Jun 5 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.