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?extracting specific substrings from stringReading 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 patternChecking duplicate value in specific column in csv file with pipe seperatedGrep a list of words from pairs of filesHow to grep lines which have more than specific number of special charactersremove specific characters from a string
Is it a good idea to teach algorithm courses using pseudocode instead of a real programming language?
How do we explain the use of a software on a math paper?
pwaS eht tirsf dna tasl setterl fo hace dorw
Addressing an email
What were the "pills" that were added to solid waste in Apollo 7?
Why is so much ransomware breakable?
Latin words remembered from high school 50 years ago
Why did Nick Fury not hesitate in blowing up the plane he thought was carrying a nuke?
Good examples of "two is easy, three is hard" in computational sciences
How to fix "webpack Dev Server Invalid Options" in Vuejs
Would it be possible to set up a franchise in the ancient world?
Print characters from list with a For-loop
Can 2 light bulbs of 120V in series be used on 230V AC?
Precedent for disabled Kings
Why does Taylor’s series “work”?
Greek theta instead of lower case þ (Icelandic) in TexStudio
Why would Thor need to strike a building with lightning to attack enemies?
Can a Warforged have a ranged weapon affixed to them like an armblade?
Is it possible to view all the attribute data in QGIS
How could Dwarves prevent sand from filling up their settlements
Have I found a major security issue with login
Isn't Kirchhoff's junction law a violation of conservation of charge?
How to plot a surface from a system of equations?
What halachos of mourning apply to a grandchild for his grandparent's death?
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?extracting specific substrings from stringReading 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 patternChecking duplicate value in specific column in csv file with pipe seperatedGrep a list of words from pairs of filesHow to grep lines which have more than specific number of special charactersremove specific characters from a string
.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 seperated by "," like-
KEY1:VALUE1, KEY2:VALUE2, KEY3:VALUE3
And from this string I have to grep value for a specific string,lets say KEY2, so output of our command should be VALUE2.
shell-script awk sed
New contributor
Abhijeet srivastava is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
I have a string which is concatenation of "key":"value" pairs seperated by "," like-
KEY1:VALUE1, KEY2:VALUE2, KEY3:VALUE3
And from this string I have to grep value for a specific string,lets say KEY2, so output of our command should be VALUE2.
shell-script awk sed
New contributor
Abhijeet srivastava is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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 seperated by "," like-
KEY1:VALUE1, KEY2:VALUE2, KEY3:VALUE3
And from this string I have to grep value for a specific string,lets say KEY2, so output of our command should be VALUE2.
shell-script awk sed
New contributor
Abhijeet srivastava is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I have a string which is concatenation of "key":"value" pairs seperated by "," like-
KEY1:VALUE1, KEY2:VALUE2, KEY3:VALUE3
And from this string I have to grep value for a specific string,lets say KEY2, so output of our command should be VALUE2.
shell-script awk sed
shell-script awk sed
New contributor
Abhijeet srivastava is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Abhijeet srivastava is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Abhijeet srivastava is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked May 13 at 12:49
Abhijeet srivastavaAbhijeet srivastava
111
111
New contributor
Abhijeet srivastava is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Abhijeet srivastava is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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 |
3 Answers
3
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 |
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/3.0/"u003ecc by-sa 3.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
);
);
Abhijeet srivastava is a new contributor. Be nice, and check out our Code of Conduct.
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
3 Answers
3
active
oldest
votes
3 Answers
3
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
5,450925
5,450925
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
12.4k13963
12.4k13963
@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,388415
1,388415
add a comment |
add a comment |
Abhijeet srivastava is a new contributor. Be nice, and check out our Code of Conduct.
Abhijeet srivastava is a new contributor. Be nice, and check out our Code of Conduct.
Abhijeet srivastava is a new contributor. Be nice, and check out our Code of Conduct.
Abhijeet srivastava is a new contributor. Be nice, and check out our Code of Conduct.
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