Any way to extract/cut a common string out of 2 different strings?
Clash Royale CLAN TAG#URR8PPP
What is Bash or Linux command for extracting the common string out of 2 partly similar, partly different strings
how to get variable
a="How good is it to"
out of
b="How good is it to die in defending fatherland"
c="How good is it to live in dedicating oneself to nation"
linux bash shell
add a comment |
What is Bash or Linux command for extracting the common string out of 2 partly similar, partly different strings
how to get variable
a="How good is it to"
out of
b="How good is it to die in defending fatherland"
c="How good is it to live in dedicating oneself to nation"
linux bash shell
Why is the wordto
not included in the string that you want to extract? What about the wordin
?
– Kusalananda
Jan 29 at 10:07
What about the trailing space?
– Jeff Schaller
Jan 29 at 10:44
Does the order of the words matter? What should the two stringsa b c 1 2 3
anda b 1 2 c 3
result in?
– Kusalananda
Jan 29 at 11:01
Yes, only the sequence of consecutive words are demanded
– abdan
Jan 29 at 11:14
Did any of the answers solved your problem? If not, let us know; otherwise, please accept it by clicking the checkmark next to it. Thank you!
– Jeff Schaller
Feb 10 at 12:51
add a comment |
What is Bash or Linux command for extracting the common string out of 2 partly similar, partly different strings
how to get variable
a="How good is it to"
out of
b="How good is it to die in defending fatherland"
c="How good is it to live in dedicating oneself to nation"
linux bash shell
What is Bash or Linux command for extracting the common string out of 2 partly similar, partly different strings
how to get variable
a="How good is it to"
out of
b="How good is it to die in defending fatherland"
c="How good is it to live in dedicating oneself to nation"
linux bash shell
linux bash shell
edited Jan 29 at 10:08
abdan
asked Jan 29 at 10:05
abdanabdan
314
314
Why is the wordto
not included in the string that you want to extract? What about the wordin
?
– Kusalananda
Jan 29 at 10:07
What about the trailing space?
– Jeff Schaller
Jan 29 at 10:44
Does the order of the words matter? What should the two stringsa b c 1 2 3
anda b 1 2 c 3
result in?
– Kusalananda
Jan 29 at 11:01
Yes, only the sequence of consecutive words are demanded
– abdan
Jan 29 at 11:14
Did any of the answers solved your problem? If not, let us know; otherwise, please accept it by clicking the checkmark next to it. Thank you!
– Jeff Schaller
Feb 10 at 12:51
add a comment |
Why is the wordto
not included in the string that you want to extract? What about the wordin
?
– Kusalananda
Jan 29 at 10:07
What about the trailing space?
– Jeff Schaller
Jan 29 at 10:44
Does the order of the words matter? What should the two stringsa b c 1 2 3
anda b 1 2 c 3
result in?
– Kusalananda
Jan 29 at 11:01
Yes, only the sequence of consecutive words are demanded
– abdan
Jan 29 at 11:14
Did any of the answers solved your problem? If not, let us know; otherwise, please accept it by clicking the checkmark next to it. Thank you!
– Jeff Schaller
Feb 10 at 12:51
Why is the word
to
not included in the string that you want to extract? What about the word in
?– Kusalananda
Jan 29 at 10:07
Why is the word
to
not included in the string that you want to extract? What about the word in
?– Kusalananda
Jan 29 at 10:07
What about the trailing space?
– Jeff Schaller
Jan 29 at 10:44
What about the trailing space?
– Jeff Schaller
Jan 29 at 10:44
Does the order of the words matter? What should the two strings
a b c 1 2 3
and a b 1 2 c 3
result in?– Kusalananda
Jan 29 at 11:01
Does the order of the words matter? What should the two strings
a b c 1 2 3
and a b 1 2 c 3
result in?– Kusalananda
Jan 29 at 11:01
Yes, only the sequence of consecutive words are demanded
– abdan
Jan 29 at 11:14
Yes, only the sequence of consecutive words are demanded
– abdan
Jan 29 at 11:14
Did any of the answers solved your problem? If not, let us know; otherwise, please accept it by clicking the checkmark next to it. Thank you!
– Jeff Schaller
Feb 10 at 12:51
Did any of the answers solved your problem? If not, let us know; otherwise, please accept it by clicking the checkmark next to it. Thank you!
– Jeff Schaller
Feb 10 at 12:51
add a comment |
3 Answers
3
active
oldest
votes
With a bash loop over the strings:
i=0
a=
while [[ $b:i:1 == $c:i:1 ]]; do a+=$b[i]; ((++i)); done
Or, with less manipulation:
i=0
while [[ $b:i:1 == $c:i:1 ]]; do ((++i)); done
a=$b:0:i
Note that this results in the string:
printf -- '-->%s<--n' "$a"
-->How good is it to <--
... with the trailing space, since that character is common to both source strings.
add a comment |
Try the following code:
b="How good is it to die in defending fatherland"
c="How good is it to live in dedicating oneself to nation"
a=()
count=0
for i in $b[@]
do
if [ "`echo "$c[@]" | grep $i`" ]; then
a[count]=$i
count=$((count+1))
fi
done
echo $a[@]
add a comment |
i have redirected value of variable "a" and "b" to file "file1" and "file2"
Below is command i have used to fetch the common strings
Let me know for any suggestions or corrections
for i in 1..13; do awk -v i="$i" 'NR==FNRa[$i];next($i in a)print $i' file1 file2 ; done|sed '/^$/d'| perl -pne "s/n/ /g"
output
How good is it to in
the OP did not show the word "in" as sample output...
– Jeff Schaller
Jan 29 at 11:39
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%2f497406%2fany-way-to-extract-cut-a-common-string-out-of-2-different-strings%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
With a bash loop over the strings:
i=0
a=
while [[ $b:i:1 == $c:i:1 ]]; do a+=$b[i]; ((++i)); done
Or, with less manipulation:
i=0
while [[ $b:i:1 == $c:i:1 ]]; do ((++i)); done
a=$b:0:i
Note that this results in the string:
printf -- '-->%s<--n' "$a"
-->How good is it to <--
... with the trailing space, since that character is common to both source strings.
add a comment |
With a bash loop over the strings:
i=0
a=
while [[ $b:i:1 == $c:i:1 ]]; do a+=$b[i]; ((++i)); done
Or, with less manipulation:
i=0
while [[ $b:i:1 == $c:i:1 ]]; do ((++i)); done
a=$b:0:i
Note that this results in the string:
printf -- '-->%s<--n' "$a"
-->How good is it to <--
... with the trailing space, since that character is common to both source strings.
add a comment |
With a bash loop over the strings:
i=0
a=
while [[ $b:i:1 == $c:i:1 ]]; do a+=$b[i]; ((++i)); done
Or, with less manipulation:
i=0
while [[ $b:i:1 == $c:i:1 ]]; do ((++i)); done
a=$b:0:i
Note that this results in the string:
printf -- '-->%s<--n' "$a"
-->How good is it to <--
... with the trailing space, since that character is common to both source strings.
With a bash loop over the strings:
i=0
a=
while [[ $b:i:1 == $c:i:1 ]]; do a+=$b[i]; ((++i)); done
Or, with less manipulation:
i=0
while [[ $b:i:1 == $c:i:1 ]]; do ((++i)); done
a=$b:0:i
Note that this results in the string:
printf -- '-->%s<--n' "$a"
-->How good is it to <--
... with the trailing space, since that character is common to both source strings.
answered Jan 29 at 14:17
Jeff SchallerJeff Schaller
41.6k1056132
41.6k1056132
add a comment |
add a comment |
Try the following code:
b="How good is it to die in defending fatherland"
c="How good is it to live in dedicating oneself to nation"
a=()
count=0
for i in $b[@]
do
if [ "`echo "$c[@]" | grep $i`" ]; then
a[count]=$i
count=$((count+1))
fi
done
echo $a[@]
add a comment |
Try the following code:
b="How good is it to die in defending fatherland"
c="How good is it to live in dedicating oneself to nation"
a=()
count=0
for i in $b[@]
do
if [ "`echo "$c[@]" | grep $i`" ]; then
a[count]=$i
count=$((count+1))
fi
done
echo $a[@]
add a comment |
Try the following code:
b="How good is it to die in defending fatherland"
c="How good is it to live in dedicating oneself to nation"
a=()
count=0
for i in $b[@]
do
if [ "`echo "$c[@]" | grep $i`" ]; then
a[count]=$i
count=$((count+1))
fi
done
echo $a[@]
Try the following code:
b="How good is it to die in defending fatherland"
c="How good is it to live in dedicating oneself to nation"
a=()
count=0
for i in $b[@]
do
if [ "`echo "$c[@]" | grep $i`" ]; then
a[count]=$i
count=$((count+1))
fi
done
echo $a[@]
answered Jan 29 at 10:58
msp9011msp9011
4,33444065
4,33444065
add a comment |
add a comment |
i have redirected value of variable "a" and "b" to file "file1" and "file2"
Below is command i have used to fetch the common strings
Let me know for any suggestions or corrections
for i in 1..13; do awk -v i="$i" 'NR==FNRa[$i];next($i in a)print $i' file1 file2 ; done|sed '/^$/d'| perl -pne "s/n/ /g"
output
How good is it to in
the OP did not show the word "in" as sample output...
– Jeff Schaller
Jan 29 at 11:39
add a comment |
i have redirected value of variable "a" and "b" to file "file1" and "file2"
Below is command i have used to fetch the common strings
Let me know for any suggestions or corrections
for i in 1..13; do awk -v i="$i" 'NR==FNRa[$i];next($i in a)print $i' file1 file2 ; done|sed '/^$/d'| perl -pne "s/n/ /g"
output
How good is it to in
the OP did not show the word "in" as sample output...
– Jeff Schaller
Jan 29 at 11:39
add a comment |
i have redirected value of variable "a" and "b" to file "file1" and "file2"
Below is command i have used to fetch the common strings
Let me know for any suggestions or corrections
for i in 1..13; do awk -v i="$i" 'NR==FNRa[$i];next($i in a)print $i' file1 file2 ; done|sed '/^$/d'| perl -pne "s/n/ /g"
output
How good is it to in
i have redirected value of variable "a" and "b" to file "file1" and "file2"
Below is command i have used to fetch the common strings
Let me know for any suggestions or corrections
for i in 1..13; do awk -v i="$i" 'NR==FNRa[$i];next($i in a)print $i' file1 file2 ; done|sed '/^$/d'| perl -pne "s/n/ /g"
output
How good is it to in
answered Jan 29 at 10:59
Praveen Kumar BSPraveen Kumar BS
1,470138
1,470138
the OP did not show the word "in" as sample output...
– Jeff Schaller
Jan 29 at 11:39
add a comment |
the OP did not show the word "in" as sample output...
– Jeff Schaller
Jan 29 at 11:39
the OP did not show the word "in" as sample output...
– Jeff Schaller
Jan 29 at 11:39
the OP did not show the word "in" as sample output...
– Jeff Schaller
Jan 29 at 11:39
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%2f497406%2fany-way-to-extract-cut-a-common-string-out-of-2-different-strings%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
Why is the word
to
not included in the string that you want to extract? What about the wordin
?– Kusalananda
Jan 29 at 10:07
What about the trailing space?
– Jeff Schaller
Jan 29 at 10:44
Does the order of the words matter? What should the two strings
a b c 1 2 3
anda b 1 2 c 3
result in?– Kusalananda
Jan 29 at 11:01
Yes, only the sequence of consecutive words are demanded
– abdan
Jan 29 at 11:14
Did any of the answers solved your problem? If not, let us know; otherwise, please accept it by clicking the checkmark next to it. Thank you!
– Jeff Schaller
Feb 10 at 12:51