bash + find specific word in space-delimited string
The parameter list include the following example values
# echo $list
master01.montil131.com worker01.montil131.com worker02.montil131.com
In order to find the word that include master string I do the following
list=$( for i in ` echo $list `; do [[ $i =~ master ]] && echo $i ; done )
echo $list
master01.montil131.com
but this approach to find the string with master word isn't elegant way
Any other suggestion how to find specific word in list ?
linux bash shell-script awk sed
add a comment |
The parameter list include the following example values
# echo $list
master01.montil131.com worker01.montil131.com worker02.montil131.com
In order to find the word that include master string I do the following
list=$( for i in ` echo $list `; do [[ $i =~ master ]] && echo $i ; done )
echo $list
master01.montil131.com
but this approach to find the string with master word isn't elegant way
Any other suggestion how to find specific word in list ?
linux bash shell-script awk sed
is$lista variable that contains the string "master01.montil131.com worker01.montil131.com worker02.montil131.com" ? a space separated list of words stored in a variable?
– glenn jackman
7 hours ago
just one space between words ( no other comma separator )'
– yael
7 hours ago
Unrelated: Why do you store a list in a string? It would be better to have it in an array...
– Kusalananda
1 hour ago
add a comment |
The parameter list include the following example values
# echo $list
master01.montil131.com worker01.montil131.com worker02.montil131.com
In order to find the word that include master string I do the following
list=$( for i in ` echo $list `; do [[ $i =~ master ]] && echo $i ; done )
echo $list
master01.montil131.com
but this approach to find the string with master word isn't elegant way
Any other suggestion how to find specific word in list ?
linux bash shell-script awk sed
The parameter list include the following example values
# echo $list
master01.montil131.com worker01.montil131.com worker02.montil131.com
In order to find the word that include master string I do the following
list=$( for i in ` echo $list `; do [[ $i =~ master ]] && echo $i ; done )
echo $list
master01.montil131.com
but this approach to find the string with master word isn't elegant way
Any other suggestion how to find specific word in list ?
linux bash shell-script awk sed
linux bash shell-script awk sed
edited 1 hour ago
Kusalananda
131k17249408
131k17249408
asked 7 hours ago
yaelyael
2,58712570
2,58712570
is$lista variable that contains the string "master01.montil131.com worker01.montil131.com worker02.montil131.com" ? a space separated list of words stored in a variable?
– glenn jackman
7 hours ago
just one space between words ( no other comma separator )'
– yael
7 hours ago
Unrelated: Why do you store a list in a string? It would be better to have it in an array...
– Kusalananda
1 hour ago
add a comment |
is$lista variable that contains the string "master01.montil131.com worker01.montil131.com worker02.montil131.com" ? a space separated list of words stored in a variable?
– glenn jackman
7 hours ago
just one space between words ( no other comma separator )'
– yael
7 hours ago
Unrelated: Why do you store a list in a string? It would be better to have it in an array...
– Kusalananda
1 hour ago
is
$list a variable that contains the string "master01.montil131.com worker01.montil131.com worker02.montil131.com" ? a space separated list of words stored in a variable?– glenn jackman
7 hours ago
is
$list a variable that contains the string "master01.montil131.com worker01.montil131.com worker02.montil131.com" ? a space separated list of words stored in a variable?– glenn jackman
7 hours ago
just one space between words ( no other comma separator )'
– yael
7 hours ago
just one space between words ( no other comma separator )'
– yael
7 hours ago
Unrelated: Why do you store a list in a string? It would be better to have it in an array...
– Kusalananda
1 hour ago
Unrelated: Why do you store a list in a string? It would be better to have it in an array...
– Kusalananda
1 hour ago
add a comment |
3 Answers
3
active
oldest
votes
Why not use simple grep command, I am assuming there are no spaces in a word in the list.
echo $list | tr ' ' 'n' | grep master
It will replace space with new line and will then grep word master.
add a comment |
Use grep -o:
grep -o "[^ ]*master[^ ]*" <<<"$list"
If you know that you always have just master* and worker*, you can use Shell methods:
echo "${list// *worker[^ ]*/}"
add a comment |
grep -ow 'master[^ ]*' <<<"$list"
or, with GNU grep,
grep -Pow 'masterS+' <<<"$list"
The -o would extract the matching bit of the string in $list, and -w would ensure that we don't match themaster or some other word that does not start with master.
The S in the second command is a PCRE that will match any non-space character.
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
});
}
});
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%2f501609%2fbash-find-specific-word-in-space-delimited-string%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
Why not use simple grep command, I am assuming there are no spaces in a word in the list.
echo $list | tr ' ' 'n' | grep master
It will replace space with new line and will then grep word master.
add a comment |
Why not use simple grep command, I am assuming there are no spaces in a word in the list.
echo $list | tr ' ' 'n' | grep master
It will replace space with new line and will then grep word master.
add a comment |
Why not use simple grep command, I am assuming there are no spaces in a word in the list.
echo $list | tr ' ' 'n' | grep master
It will replace space with new line and will then grep word master.
Why not use simple grep command, I am assuming there are no spaces in a word in the list.
echo $list | tr ' ' 'n' | grep master
It will replace space with new line and will then grep word master.
edited 7 hours ago
answered 7 hours ago
PRYPRY
2,43031025
2,43031025
add a comment |
add a comment |
Use grep -o:
grep -o "[^ ]*master[^ ]*" <<<"$list"
If you know that you always have just master* and worker*, you can use Shell methods:
echo "${list// *worker[^ ]*/}"
add a comment |
Use grep -o:
grep -o "[^ ]*master[^ ]*" <<<"$list"
If you know that you always have just master* and worker*, you can use Shell methods:
echo "${list// *worker[^ ]*/}"
add a comment |
Use grep -o:
grep -o "[^ ]*master[^ ]*" <<<"$list"
If you know that you always have just master* and worker*, you can use Shell methods:
echo "${list// *worker[^ ]*/}"
Use grep -o:
grep -o "[^ ]*master[^ ]*" <<<"$list"
If you know that you always have just master* and worker*, you can use Shell methods:
echo "${list// *worker[^ ]*/}"
edited 7 hours ago
answered 7 hours ago
RoVoRoVo
3,161216
3,161216
add a comment |
add a comment |
grep -ow 'master[^ ]*' <<<"$list"
or, with GNU grep,
grep -Pow 'masterS+' <<<"$list"
The -o would extract the matching bit of the string in $list, and -w would ensure that we don't match themaster or some other word that does not start with master.
The S in the second command is a PCRE that will match any non-space character.
add a comment |
grep -ow 'master[^ ]*' <<<"$list"
or, with GNU grep,
grep -Pow 'masterS+' <<<"$list"
The -o would extract the matching bit of the string in $list, and -w would ensure that we don't match themaster or some other word that does not start with master.
The S in the second command is a PCRE that will match any non-space character.
add a comment |
grep -ow 'master[^ ]*' <<<"$list"
or, with GNU grep,
grep -Pow 'masterS+' <<<"$list"
The -o would extract the matching bit of the string in $list, and -w would ensure that we don't match themaster or some other word that does not start with master.
The S in the second command is a PCRE that will match any non-space character.
grep -ow 'master[^ ]*' <<<"$list"
or, with GNU grep,
grep -Pow 'masterS+' <<<"$list"
The -o would extract the matching bit of the string in $list, and -w would ensure that we don't match themaster or some other word that does not start with master.
The S in the second command is a PCRE that will match any non-space character.
answered 49 mins ago
KusalanandaKusalananda
131k17249408
131k17249408
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%2f501609%2fbash-find-specific-word-in-space-delimited-string%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
is
$lista variable that contains the string "master01.montil131.com worker01.montil131.com worker02.montil131.com" ? a space separated list of words stored in a variable?– glenn jackman
7 hours ago
just one space between words ( no other comma separator )'
– yael
7 hours ago
Unrelated: Why do you store a list in a string? It would be better to have it in an array...
– Kusalananda
1 hour ago