I have a full list of API status with lots of information like that. I need to grep the name of the username so I can send it to another file or variable.

For example:


I need to grep the string after "username": In the end to have only ivan.ivanov or whatever the name will be.

  • yes it is a full json, I just don't show the whole one as it is has sensitive information. Just giving a small part of it, if that is enough – WhoAmI Nov 21 '18 at 14:48
  • okay, that's good, then you can use a json parser. Anyways, in your question you should provide a valid example of your input, even when shortened. – pLumo Nov 21 '18 at 14:50

If this was a proper json string, you could parse it with jq:

your_api_call | jq -r '.[]["username"]'


jq -r '.[]["username"]' file

But the string you provided is not proper json. It's missing the closing brackets (]}) at the end and has a . instead.

jq should be available in most package managers, e.g. install it with:

sudo apt install jq

If you somehow need to use grep and have pgrep / grep -P available:

grep -Po '"username":"\K[^"]*'
  • Actually that works, but shows me for only the first occurrence. Can it be done for every string that contains username? * UPDATE* IT Works with grep , thanks a lot – WhoAmI Nov 21 '18 at 14:50
  • did you see my update using [] instead of [0] ? – pLumo Nov 21 '18 at 14:51

I have used the following command and it worked fine:

echo "[{"id":"1onyc4b1otgmtrmw37h83rjs9w","create_at":1542718790947,"update_at":1542728017634,"delete_at":0,"username":"ivan.ivanov","auth_data":""." |
  sed "s/,/\n/g" |
  awk -F ":" '/username/{print $NF}'



alternatively you could do with jtc:

bash $ jtc -w'<username>l+0' your.json
bash $ 

if your json has multiple usernames all will be printed, otherwise (if you need just one) drop '+0' - the first only will be printed

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.