Grep value of a specific key from a String, concatenated of key : value pairsReversing the value key pairs of array using sed or pattern replacement or brace expansion?Reading a string till a key word and replacing from there with another stringMatching “keyword value” pairs from semi-structured inputComplete key value pairssed/awk replace a specific pattern under another patternHow to grep two strings in line by specific order AND calculating values line by line according to my grepHow to grep lines which have more than specific number of special charactersHow to select specific columns from a file with a string separator
Car as a good investment
Latest newtx package update (v1.601 Oct 2, 2019) breaks the footnote command [update: bug fixed by package author]
How many wires can safely be secured in a Marrette 33 wire nut?
Code Golf Measurer © 2019
Did Terry Pratchett ever explain the inspiration behind the Luggage?
Get injured / Get increased
Why do these two ways of understanding constant acceleration give different results?
How can you tell apart the pronounciation at the end between the "meine" and "meiner" in the daily spoken situation?
How are Aircraft Noses Designed?
How to extract *.tgz.part-*?
Is oxygen above the critical point always supercritical fluid? Would it still appear to roughly follow the ideal gas law?
Generating sequential alphanumeric values that match a certain pattern
How (and if) to include name change for transgender person in genealogy?
How to deal with people whose priority is to not get blamed?
What are the differences between prismatic, lensatic, and optical sighting compasses?
Do more Americans want the Bidens investigated than Trump impeached?
Can we not simply connect a battery to a RAM to prevent data loss during power cuts?
How do I remove 'None' items from the end of a list in Python
Can a species eat water?
What fantasy book has twins (except one's blue) and a cloaked ice bear on the cover?
Does immunity to fear prevent a mummy's Dreadful Glare from paralyzing a character?
Does Darwin owe a debt to Hegel?
Which culture used no personal names?
Idiom for a situation or event that makes one poor or even poorer?
Grep value of a specific key from a String, concatenated of key : value pairs
Reversing the value key pairs of array using sed or pattern replacement or brace expansion?Reading a string till a key word and replacing from there with another stringMatching “keyword value” pairs from semi-structured inputComplete key value pairssed/awk replace a specific pattern under another patternHow to grep two strings in line by specific order AND calculating values line by line according to my grepHow to grep lines which have more than specific number of special charactersHow to select specific columns from a file with a string separator
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;
I have a string which is concatenation of "key":"value" pairs separated by "," like-
KEY1:VALUE1, KEY2:VALUE2, KEY3:VALUE3
From this string, I have to grep for a specific string -- let's say KEY2 -- so the output of our command should be VALUE2.
shell-script awk sed
add a comment
|
I have a string which is concatenation of "key":"value" pairs separated by "," like-
KEY1:VALUE1, KEY2:VALUE2, KEY3:VALUE3
From this string, I have to grep for a specific string -- let's say KEY2 -- so the output of our command should be VALUE2.
shell-script awk sed
3
Can the values contain quotes commas or colons? Is it JSON?
– Jeff Schaller♦
May 13 at 12:54
add a comment
|
I have a string which is concatenation of "key":"value" pairs separated by "," like-
KEY1:VALUE1, KEY2:VALUE2, KEY3:VALUE3
From this string, I have to grep for a specific string -- let's say KEY2 -- so the output of our command should be VALUE2.
shell-script awk sed
I have a string which is concatenation of "key":"value" pairs separated by "," like-
KEY1:VALUE1, KEY2:VALUE2, KEY3:VALUE3
From this string, I have to grep for a specific string -- let's say KEY2 -- so the output of our command should be VALUE2.
shell-script awk sed
shell-script awk sed
edited May 19 at 10:43
Jeff Schaller♦
50.3k11 gold badges74 silver badges167 bronze badges
50.3k11 gold badges74 silver badges167 bronze badges
asked May 13 at 12:49
Abhijeet srivastavaAbhijeet srivastava
111 bronze badge
111 bronze badge
3
Can the values contain quotes commas or colons? Is it JSON?
– Jeff Schaller♦
May 13 at 12:54
add a comment
|
3
Can the values contain quotes commas or colons? Is it JSON?
– Jeff Schaller♦
May 13 at 12:54
3
3
Can the values contain quotes commas or colons? Is it JSON?
– Jeff Schaller♦
May 13 at 12:54
Can the values contain quotes commas or colons? Is it JSON?
– Jeff Schaller♦
May 13 at 12:54
add a comment
|
4 Answers
4
active
oldest
votes
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"
add a comment
|
With regular grep
assuming VALUE doesn't contain a colon:
grep -o 'KEY2:[^,]+' | grep -o '[^:]+$'
@RoVo: added caveat
– Thor
May 13 at 14:25
add a comment
|
To grep only the value
echo $myString | grep -Po "(?<=KEY2:)[^,]*"
or
grep -Po "(?<=KEY2:)[^,]*" <<< $myString
add a comment
|
Use awk, read key:value pair as record, read key, value as 1st and 2nd field.
awk -v RS=' *, *' -v FS=' *: *' '$1=="KEY2"print $2' <<<$str
-v RS=' *, *'
set record separator to,
and it's surrounding space-v FS=' *: *'
set field separator to:
and it's surrounding space'$1=="KEY2"print $2'
print value if key found. change "KEY2" to your desired key value.
add a comment
|
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f518673%2fgrep-value-of-a-specific-key-from-a-string-concatenated-of-key-value-pairs%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
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"
add a comment
|
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"
add a comment
|
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"
edited May 14 at 9:02
answered May 13 at 12:56
pLumopLumo
7,93115 silver badges36 bronze badges
7,93115 silver badges36 bronze badges
add a comment
|
add a comment
|
With regular grep
assuming VALUE doesn't contain a colon:
grep -o 'KEY2:[^,]+' | grep -o '[^:]+$'
@RoVo: added caveat
– Thor
May 13 at 14:25
add a comment
|
With regular grep
assuming VALUE doesn't contain a colon:
grep -o 'KEY2:[^,]+' | grep -o '[^:]+$'
@RoVo: added caveat
– Thor
May 13 at 14:25
add a comment
|
With regular grep
assuming VALUE doesn't contain a colon:
grep -o 'KEY2:[^,]+' | grep -o '[^:]+$'
With regular grep
assuming VALUE doesn't contain a colon:
grep -o 'KEY2:[^,]+' | grep -o '[^:]+$'
edited May 13 at 14:24
answered May 13 at 14:18
ThorThor
13.1k1 gold badge41 silver badges64 bronze badges
13.1k1 gold badge41 silver badges64 bronze badges
@RoVo: added caveat
– Thor
May 13 at 14:25
add a comment
|
@RoVo: added caveat
– Thor
May 13 at 14:25
@RoVo: added caveat
– Thor
May 13 at 14:25
@RoVo: added caveat
– Thor
May 13 at 14:25
add a comment
|
To grep only the value
echo $myString | grep -Po "(?<=KEY2:)[^,]*"
or
grep -Po "(?<=KEY2:)[^,]*" <<< $myString
add a comment
|
To grep only the value
echo $myString | grep -Po "(?<=KEY2:)[^,]*"
or
grep -Po "(?<=KEY2:)[^,]*" <<< $myString
add a comment
|
To grep only the value
echo $myString | grep -Po "(?<=KEY2:)[^,]*"
or
grep -Po "(?<=KEY2:)[^,]*" <<< $myString
To grep only the value
echo $myString | grep -Po "(?<=KEY2:)[^,]*"
or
grep -Po "(?<=KEY2:)[^,]*" <<< $myString
answered May 13 at 14:52
bu5hmanbu5hman
1,7331 gold badge4 silver badges15 bronze badges
1,7331 gold badge4 silver badges15 bronze badges
add a comment
|
add a comment
|
Use awk, read key:value pair as record, read key, value as 1st and 2nd field.
awk -v RS=' *, *' -v FS=' *: *' '$1=="KEY2"print $2' <<<$str
-v RS=' *, *'
set record separator to,
and it's surrounding space-v FS=' *: *'
set field separator to:
and it's surrounding space'$1=="KEY2"print $2'
print value if key found. change "KEY2" to your desired key value.
add a comment
|
Use awk, read key:value pair as record, read key, value as 1st and 2nd field.
awk -v RS=' *, *' -v FS=' *: *' '$1=="KEY2"print $2' <<<$str
-v RS=' *, *'
set record separator to,
and it's surrounding space-v FS=' *: *'
set field separator to:
and it's surrounding space'$1=="KEY2"print $2'
print value if key found. change "KEY2" to your desired key value.
add a comment
|
Use awk, read key:value pair as record, read key, value as 1st and 2nd field.
awk -v RS=' *, *' -v FS=' *: *' '$1=="KEY2"print $2' <<<$str
-v RS=' *, *'
set record separator to,
and it's surrounding space-v FS=' *: *'
set field separator to:
and it's surrounding space'$1=="KEY2"print $2'
print value if key found. change "KEY2" to your desired key value.
Use awk, read key:value pair as record, read key, value as 1st and 2nd field.
awk -v RS=' *, *' -v FS=' *: *' '$1=="KEY2"print $2' <<<$str
-v RS=' *, *'
set record separator to,
and it's surrounding space-v FS=' *: *'
set field separator to:
and it's surrounding space'$1=="KEY2"print $2'
print value if key found. change "KEY2" to your desired key value.
answered May 19 at 11:21
dedowsdidedowsdi
8702 silver badges9 bronze badges
8702 silver badges9 bronze badges
add a comment
|
add a comment
|
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f518673%2fgrep-value-of-a-specific-key-from-a-string-concatenated-of-key-value-pairs%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
Can the values contain quotes commas or colons? Is it JSON?
– Jeff Schaller♦
May 13 at 12:54