grep : find bigger string in small string
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I am writing a small shell script to receive an input and match against a text file.
However, the input would be a bigger string than the string in the file, against which I want a match.
User input:
abc-de-ef-gh1
Text in file:
This is test,-de-ef-gh,This is test
I want a positive result if I grep
user-input with existing file.
How can I achieve this? I couldn't do it with grep.
linux shell grep regular-expression
 |Â
show 2 more comments
up vote
1
down vote
favorite
I am writing a small shell script to receive an input and match against a text file.
However, the input would be a bigger string than the string in the file, against which I want a match.
User input:
abc-de-ef-gh1
Text in file:
This is test,-de-ef-gh,This is test
I want a positive result if I grep
user-input with existing file.
How can I achieve this? I couldn't do it with grep.
linux shell grep regular-expression
2
how about echo "user input" | grep -f pattern_file
â Kamaraj
Aug 31 at 9:09
Kamaraj - it doesn't work.
â Novice
Aug 31 at 9:15
1
If the user input and the pattern are as in the question, then the comment of @Kamaraj will work. So your problem is not reproducible. Please give exact input and patterns. "It doesn't work" will not help us to help you.
â chaos
Aug 31 at 9:37
Apologies as I was unclear. Edited the question.
â Novice
Aug 31 at 9:54
1
This question is unclear. I sort-of understand it, but not clearly enough for it to be answerable.â @Novice:â¯Doâ¯you want to match a line that containsabc-d-ef-gh1
?âÂÂHow aboutf-gh
?âÂÂHow about-gh1
?âÂÂHow aboutgh
or1
?â Describe the processes by which you would answer those questions; i.e., describe the rules that determine whether a string should match a line.âÂÂPleaseâ¯doâ¯not respond inâ¯comments; edit your question toâ¯make it clearer andâ¯more complete.
â G-Man
Sep 2 at 20:59
 |Â
show 2 more comments
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am writing a small shell script to receive an input and match against a text file.
However, the input would be a bigger string than the string in the file, against which I want a match.
User input:
abc-de-ef-gh1
Text in file:
This is test,-de-ef-gh,This is test
I want a positive result if I grep
user-input with existing file.
How can I achieve this? I couldn't do it with grep.
linux shell grep regular-expression
I am writing a small shell script to receive an input and match against a text file.
However, the input would be a bigger string than the string in the file, against which I want a match.
User input:
abc-de-ef-gh1
Text in file:
This is test,-de-ef-gh,This is test
I want a positive result if I grep
user-input with existing file.
How can I achieve this? I couldn't do it with grep.
linux shell grep regular-expression
linux shell grep regular-expression
edited Aug 31 at 9:51
asked Aug 31 at 9:07
Novice
92
92
2
how about echo "user input" | grep -f pattern_file
â Kamaraj
Aug 31 at 9:09
Kamaraj - it doesn't work.
â Novice
Aug 31 at 9:15
1
If the user input and the pattern are as in the question, then the comment of @Kamaraj will work. So your problem is not reproducible. Please give exact input and patterns. "It doesn't work" will not help us to help you.
â chaos
Aug 31 at 9:37
Apologies as I was unclear. Edited the question.
â Novice
Aug 31 at 9:54
1
This question is unclear. I sort-of understand it, but not clearly enough for it to be answerable.â @Novice:â¯Doâ¯you want to match a line that containsabc-d-ef-gh1
?âÂÂHow aboutf-gh
?âÂÂHow about-gh1
?âÂÂHow aboutgh
or1
?â Describe the processes by which you would answer those questions; i.e., describe the rules that determine whether a string should match a line.âÂÂPleaseâ¯doâ¯not respond inâ¯comments; edit your question toâ¯make it clearer andâ¯more complete.
â G-Man
Sep 2 at 20:59
 |Â
show 2 more comments
2
how about echo "user input" | grep -f pattern_file
â Kamaraj
Aug 31 at 9:09
Kamaraj - it doesn't work.
â Novice
Aug 31 at 9:15
1
If the user input and the pattern are as in the question, then the comment of @Kamaraj will work. So your problem is not reproducible. Please give exact input and patterns. "It doesn't work" will not help us to help you.
â chaos
Aug 31 at 9:37
Apologies as I was unclear. Edited the question.
â Novice
Aug 31 at 9:54
1
This question is unclear. I sort-of understand it, but not clearly enough for it to be answerable.â @Novice:â¯Doâ¯you want to match a line that containsabc-d-ef-gh1
?âÂÂHow aboutf-gh
?âÂÂHow about-gh1
?âÂÂHow aboutgh
or1
?â Describe the processes by which you would answer those questions; i.e., describe the rules that determine whether a string should match a line.âÂÂPleaseâ¯doâ¯not respond inâ¯comments; edit your question toâ¯make it clearer andâ¯more complete.
â G-Man
Sep 2 at 20:59
2
2
how about echo "user input" | grep -f pattern_file
â Kamaraj
Aug 31 at 9:09
how about echo "user input" | grep -f pattern_file
â Kamaraj
Aug 31 at 9:09
Kamaraj - it doesn't work.
â Novice
Aug 31 at 9:15
Kamaraj - it doesn't work.
â Novice
Aug 31 at 9:15
1
1
If the user input and the pattern are as in the question, then the comment of @Kamaraj will work. So your problem is not reproducible. Please give exact input and patterns. "It doesn't work" will not help us to help you.
â chaos
Aug 31 at 9:37
If the user input and the pattern are as in the question, then the comment of @Kamaraj will work. So your problem is not reproducible. Please give exact input and patterns. "It doesn't work" will not help us to help you.
â chaos
Aug 31 at 9:37
Apologies as I was unclear. Edited the question.
â Novice
Aug 31 at 9:54
Apologies as I was unclear. Edited the question.
â Novice
Aug 31 at 9:54
1
1
This question is unclear. I sort-of understand it, but not clearly enough for it to be answerable.â @Novice:â¯Doâ¯you want to match a line that contains
abc-d-ef-gh1
?âÂÂHow about f-gh
?âÂÂHow about -gh1
?âÂÂHow about gh
or 1
?â Describe the processes by which you would answer those questions; i.e., describe the rules that determine whether a string should match a line.âÂÂPleaseâ¯doâ¯not respond inâ¯comments; edit your question toâ¯make it clearer andâ¯more complete.â G-Man
Sep 2 at 20:59
This question is unclear. I sort-of understand it, but not clearly enough for it to be answerable.â @Novice:â¯Doâ¯you want to match a line that contains
abc-d-ef-gh1
?âÂÂHow about f-gh
?âÂÂHow about -gh1
?âÂÂHow about gh
or 1
?â Describe the processes by which you would answer those questions; i.e., describe the rules that determine whether a string should match a line.âÂÂPleaseâ¯doâ¯not respond inâ¯comments; edit your question toâ¯make it clearer andâ¯more complete.â G-Man
Sep 2 at 20:59
 |Â
show 2 more comments
2 Answers
2
active
oldest
votes
up vote
1
down vote
You have a file with patterns. grep
can read the patterns with its -f
option. If you want to check whether a string, $input
, matches a pattern in patterns.txt
, you may do
printf '%sn' "$input" |
if grep -q -f patterns.txt; then
echo 'matches'
else
echo 'does not match'
fi
This would run grep
over the contents of the string with the patterns in patterns.txt
. In bash
you could instead use a here-string:
if grep -q -f patterns.txt <<<"$input"; then
echo 'matches'
else
echo 'does not match'
fi
The -q
stops grep
from producing output (we're only interested in the exit status).
If the patterns are fixed strings (not regular expressions), use -F
with grep
in addition to the other flags (grep -qF -f ...
).
add a comment |Â
up vote
0
down vote
Using bash
, cut
and grep
:
read -p "Hey user, input something: " $n
printf "Match "
grep -m 1 -q -f <(cut -d, -f2 file) <<< "$n" || printf "not "
printf "found.n"
Notes:
- It's impossible to search for a longer string in a shorter, but this is really about finding whether shorter strings in file match a longer string.
cut
is used to extract the middle field from filegrep
searches for that middle field in the user input string.printf
provides some feedback.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
You have a file with patterns. grep
can read the patterns with its -f
option. If you want to check whether a string, $input
, matches a pattern in patterns.txt
, you may do
printf '%sn' "$input" |
if grep -q -f patterns.txt; then
echo 'matches'
else
echo 'does not match'
fi
This would run grep
over the contents of the string with the patterns in patterns.txt
. In bash
you could instead use a here-string:
if grep -q -f patterns.txt <<<"$input"; then
echo 'matches'
else
echo 'does not match'
fi
The -q
stops grep
from producing output (we're only interested in the exit status).
If the patterns are fixed strings (not regular expressions), use -F
with grep
in addition to the other flags (grep -qF -f ...
).
add a comment |Â
up vote
1
down vote
You have a file with patterns. grep
can read the patterns with its -f
option. If you want to check whether a string, $input
, matches a pattern in patterns.txt
, you may do
printf '%sn' "$input" |
if grep -q -f patterns.txt; then
echo 'matches'
else
echo 'does not match'
fi
This would run grep
over the contents of the string with the patterns in patterns.txt
. In bash
you could instead use a here-string:
if grep -q -f patterns.txt <<<"$input"; then
echo 'matches'
else
echo 'does not match'
fi
The -q
stops grep
from producing output (we're only interested in the exit status).
If the patterns are fixed strings (not regular expressions), use -F
with grep
in addition to the other flags (grep -qF -f ...
).
add a comment |Â
up vote
1
down vote
up vote
1
down vote
You have a file with patterns. grep
can read the patterns with its -f
option. If you want to check whether a string, $input
, matches a pattern in patterns.txt
, you may do
printf '%sn' "$input" |
if grep -q -f patterns.txt; then
echo 'matches'
else
echo 'does not match'
fi
This would run grep
over the contents of the string with the patterns in patterns.txt
. In bash
you could instead use a here-string:
if grep -q -f patterns.txt <<<"$input"; then
echo 'matches'
else
echo 'does not match'
fi
The -q
stops grep
from producing output (we're only interested in the exit status).
If the patterns are fixed strings (not regular expressions), use -F
with grep
in addition to the other flags (grep -qF -f ...
).
You have a file with patterns. grep
can read the patterns with its -f
option. If you want to check whether a string, $input
, matches a pattern in patterns.txt
, you may do
printf '%sn' "$input" |
if grep -q -f patterns.txt; then
echo 'matches'
else
echo 'does not match'
fi
This would run grep
over the contents of the string with the patterns in patterns.txt
. In bash
you could instead use a here-string:
if grep -q -f patterns.txt <<<"$input"; then
echo 'matches'
else
echo 'does not match'
fi
The -q
stops grep
from producing output (we're only interested in the exit status).
If the patterns are fixed strings (not regular expressions), use -F
with grep
in addition to the other flags (grep -qF -f ...
).
answered Aug 31 at 9:37
Kusalananda
107k14209331
107k14209331
add a comment |Â
add a comment |Â
up vote
0
down vote
Using bash
, cut
and grep
:
read -p "Hey user, input something: " $n
printf "Match "
grep -m 1 -q -f <(cut -d, -f2 file) <<< "$n" || printf "not "
printf "found.n"
Notes:
- It's impossible to search for a longer string in a shorter, but this is really about finding whether shorter strings in file match a longer string.
cut
is used to extract the middle field from filegrep
searches for that middle field in the user input string.printf
provides some feedback.
add a comment |Â
up vote
0
down vote
Using bash
, cut
and grep
:
read -p "Hey user, input something: " $n
printf "Match "
grep -m 1 -q -f <(cut -d, -f2 file) <<< "$n" || printf "not "
printf "found.n"
Notes:
- It's impossible to search for a longer string in a shorter, but this is really about finding whether shorter strings in file match a longer string.
cut
is used to extract the middle field from filegrep
searches for that middle field in the user input string.printf
provides some feedback.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Using bash
, cut
and grep
:
read -p "Hey user, input something: " $n
printf "Match "
grep -m 1 -q -f <(cut -d, -f2 file) <<< "$n" || printf "not "
printf "found.n"
Notes:
- It's impossible to search for a longer string in a shorter, but this is really about finding whether shorter strings in file match a longer string.
cut
is used to extract the middle field from filegrep
searches for that middle field in the user input string.printf
provides some feedback.
Using bash
, cut
and grep
:
read -p "Hey user, input something: " $n
printf "Match "
grep -m 1 -q -f <(cut -d, -f2 file) <<< "$n" || printf "not "
printf "found.n"
Notes:
- It's impossible to search for a longer string in a shorter, but this is really about finding whether shorter strings in file match a longer string.
cut
is used to extract the middle field from filegrep
searches for that middle field in the user input string.printf
provides some feedback.
answered Sep 2 at 5:27
agc
4,2651935
4,2651935
add a comment |Â
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f465963%2fgrep-find-bigger-string-in-small-string%23new-answer', 'question_page');
);
Post as a guest
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
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
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
2
how about echo "user input" | grep -f pattern_file
â Kamaraj
Aug 31 at 9:09
Kamaraj - it doesn't work.
â Novice
Aug 31 at 9:15
1
If the user input and the pattern are as in the question, then the comment of @Kamaraj will work. So your problem is not reproducible. Please give exact input and patterns. "It doesn't work" will not help us to help you.
â chaos
Aug 31 at 9:37
Apologies as I was unclear. Edited the question.
â Novice
Aug 31 at 9:54
1
This question is unclear. I sort-of understand it, but not clearly enough for it to be answerable.â @Novice:â¯Doâ¯you want to match a line that contains
abc-d-ef-gh1
?âÂÂHow aboutf-gh
?âÂÂHow about-gh1
?âÂÂHow aboutgh
or1
?â Describe the processes by which you would answer those questions; i.e., describe the rules that determine whether a string should match a line.âÂÂPleaseâ¯doâ¯not respond inâ¯comments; edit your question toâ¯make it clearer andâ¯more complete.â G-Man
Sep 2 at 20:59