Skip to main content
`pgrep` is *process grep*
Source Link
Stéphane Chazelas
  • 586.2k
  • 96
  • 1.1k
  • 1.7k

Using PCRE-enabled pgrepgrep implementations:

grep -Po '(^|[ ,])KEY1:\K[^,]*' 

or egrep and cut:

grep -Eo '(^|[ ,])KEY2:[^,]*' | cut -d: -f2- 

For both methods, the Value is not allowed to contain comma.


If you had proper json, e.g.

{ "KEY1":"VALUE1", "KEY2":"VALUE2", "KEY3":"VALUE3" } 

you could use jq:

$ jq .KEY2 "VALUE2" $ jq -r .KEY2 VALUE2 

Using pgrep:

grep -Po '(^|[ ,])KEY1:\K[^,]*' 

or egrep and cut:

grep -Eo '(^|[ ,])KEY2:[^,]*' | cut -d: -f2- 

For both methods, the Value is not allowed to contain comma.


If you had proper json, e.g.

{ "KEY1":"VALUE1", "KEY2":"VALUE2", "KEY3":"VALUE3" } 

you could use jq:

$ jq .KEY2 "VALUE2" $ jq -r .KEY2 VALUE2 

Using PCRE-enabled grep implementations:

grep -Po '(^|[ ,])KEY1:\K[^,]*' 

or egrep and cut:

grep -Eo '(^|[ ,])KEY2:[^,]*' | cut -d: -f2- 

For both methods, the Value is not allowed to contain comma.


If you had proper json, e.g.

{ "KEY1":"VALUE1", "KEY2":"VALUE2", "KEY3":"VALUE3" } 

you could use jq:

$ jq .KEY2 "VALUE2" $ jq -r .KEY2 VALUE2 
added 45 characters in body
Source Link
Stéphane Chazelas
  • 586.2k
  • 96
  • 1.1k
  • 1.7k

Using pgrep:

grep -Po '(^|[ ,])KEY1:\K[^,]*' 

or egrep and cut:

grep -Eo '(^|[ ,])KEY2:[^,]*' | cut -d: -f2- 

For both methods, the Value is not allowed to contain comma.


If you had proper json, e.g.

{ "KEY1":"VALUE1", "KEY2":"VALUE2", "KEY3":"VALUE3" } 

you could use jq:

$ jq ".KEY2"KEY2 "VALUE2" $ jq -r .KEY2 VALUE2 

Using pgrep:

grep -Po '(^|[ ,])KEY1:\K[^,]*' 

or egrep and cut:

grep -Eo '(^|[ ,])KEY2:[^,]*' | cut -d: -f2- 

For both methods, the Value is not allowed to contain comma.


If you had proper json, e.g.

{ "KEY1":"VALUE1", "KEY2":"VALUE2", "KEY3":"VALUE3" } 

you could use jq:

jq ".KEY2" 

Using pgrep:

grep -Po '(^|[ ,])KEY1:\K[^,]*' 

or egrep and cut:

grep -Eo '(^|[ ,])KEY2:[^,]*' | cut -d: -f2- 

For both methods, the Value is not allowed to contain comma.


If you had proper json, e.g.

{ "KEY1":"VALUE1", "KEY2":"VALUE2", "KEY3":"VALUE3" } 

you could use jq:

$ jq .KEY2 "VALUE2" $ jq -r .KEY2 VALUE2 
added 1 character in body
Source Link
pLumo
  • 23.2k
  • 2
  • 43
  • 70

Using pgrep:

grep -Po '(^|[ ,])KEY1:\K[^,]*' 

or normal grepegrep and cut:

grep -oEo '\bKEY2'(^|[ ,])KEY2:[^,]*' | cut -d: -f2- 

For both methods, the Value is not allowed to contain comma.


If you had proper json, e.g.

{ "KEY1":"VALUE1", "KEY2":"VALUE2", "KEY3":"VALUE3" } 

you could use jq:

jq ".KEY2" 

Using pgrep:

grep -Po '(^|[ ,])KEY1:\K[^,]*' 

or normal grep and cut:

grep -o '\bKEY2:[^,]*' | cut -d: -f2- 

For both methods, the Value is not allowed to contain comma.


If you had proper json, e.g.

{ "KEY1":"VALUE1", "KEY2":"VALUE2", "KEY3":"VALUE3" } 

you could use jq:

jq ".KEY2" 

Using pgrep:

grep -Po '(^|[ ,])KEY1:\K[^,]*' 

or egrep and cut:

grep -Eo '(^|[ ,])KEY2:[^,]*' | cut -d: -f2- 

For both methods, the Value is not allowed to contain comma.


If you had proper json, e.g.

{ "KEY1":"VALUE1", "KEY2":"VALUE2", "KEY3":"VALUE3" } 

you could use jq:

jq ".KEY2" 
added 8 characters in body
Source Link
pLumo
  • 23.2k
  • 2
  • 43
  • 70
Loading
Source Link
pLumo
  • 23.2k
  • 2
  • 43
  • 70
Loading