Match content by reading file using bash grep, string enclosed with [ and ] special character
I would like to match string contains [xyz] or [abc] from the file using bash grep which contains like:
TEST_FILE:
Testing of pattern line no 1 [xyz]
Hi, this is just test file contains abc and/or xyz etc..
Tried code:
if ( grep -iq "[xyz]" "$TEST_FILE" ); then
PATTERN_MATCH="xyz matched"
elif ( grep -iq "[abc]" "$TEST_FILE" ); then
PATTERN_MATCH="abc matched"
fi
The problem is, it always matches although by passing xyz/abc instead of [xyz]/[abc].
File content can match to case-insensitive [XYZ] or [ABC] but with just xyz or abc. [ and ] special character enclosed with string. Can someone point the issue or solution for same due to [ and ] special character? I tried other ways using grep and egrep too but no luck!
bash grep
add a comment |
I would like to match string contains [xyz] or [abc] from the file using bash grep which contains like:
TEST_FILE:
Testing of pattern line no 1 [xyz]
Hi, this is just test file contains abc and/or xyz etc..
Tried code:
if ( grep -iq "[xyz]" "$TEST_FILE" ); then
PATTERN_MATCH="xyz matched"
elif ( grep -iq "[abc]" "$TEST_FILE" ); then
PATTERN_MATCH="abc matched"
fi
The problem is, it always matches although by passing xyz/abc instead of [xyz]/[abc].
File content can match to case-insensitive [XYZ] or [ABC] but with just xyz or abc. [ and ] special character enclosed with string. Can someone point the issue or solution for same due to [ and ] special character? I tried other ways using grep and egrep too but no luck!
bash grep
1
What is not working?
– anubhava
Nov 21 '18 at 15:01
2
yeah, need more details, though the script can be improved (no need parantheses, use-F
for fixed string matching, use single quotes for search argument, lowercase variable names, etc), the given bash snippet doesn't seem to have an error as such..
– Sundeep
Nov 21 '18 at 15:03
add a comment |
I would like to match string contains [xyz] or [abc] from the file using bash grep which contains like:
TEST_FILE:
Testing of pattern line no 1 [xyz]
Hi, this is just test file contains abc and/or xyz etc..
Tried code:
if ( grep -iq "[xyz]" "$TEST_FILE" ); then
PATTERN_MATCH="xyz matched"
elif ( grep -iq "[abc]" "$TEST_FILE" ); then
PATTERN_MATCH="abc matched"
fi
The problem is, it always matches although by passing xyz/abc instead of [xyz]/[abc].
File content can match to case-insensitive [XYZ] or [ABC] but with just xyz or abc. [ and ] special character enclosed with string. Can someone point the issue or solution for same due to [ and ] special character? I tried other ways using grep and egrep too but no luck!
bash grep
I would like to match string contains [xyz] or [abc] from the file using bash grep which contains like:
TEST_FILE:
Testing of pattern line no 1 [xyz]
Hi, this is just test file contains abc and/or xyz etc..
Tried code:
if ( grep -iq "[xyz]" "$TEST_FILE" ); then
PATTERN_MATCH="xyz matched"
elif ( grep -iq "[abc]" "$TEST_FILE" ); then
PATTERN_MATCH="abc matched"
fi
The problem is, it always matches although by passing xyz/abc instead of [xyz]/[abc].
File content can match to case-insensitive [XYZ] or [ABC] but with just xyz or abc. [ and ] special character enclosed with string. Can someone point the issue or solution for same due to [ and ] special character? I tried other ways using grep and egrep too but no luck!
bash grep
bash grep
edited Nov 21 '18 at 15:46
Jitesh Sojitra
asked Nov 21 '18 at 14:57
Jitesh SojitraJitesh Sojitra
1,19011532
1,19011532
1
What is not working?
– anubhava
Nov 21 '18 at 15:01
2
yeah, need more details, though the script can be improved (no need parantheses, use-F
for fixed string matching, use single quotes for search argument, lowercase variable names, etc), the given bash snippet doesn't seem to have an error as such..
– Sundeep
Nov 21 '18 at 15:03
add a comment |
1
What is not working?
– anubhava
Nov 21 '18 at 15:01
2
yeah, need more details, though the script can be improved (no need parantheses, use-F
for fixed string matching, use single quotes for search argument, lowercase variable names, etc), the given bash snippet doesn't seem to have an error as such..
– Sundeep
Nov 21 '18 at 15:03
1
1
What is not working?
– anubhava
Nov 21 '18 at 15:01
What is not working?
– anubhava
Nov 21 '18 at 15:01
2
2
yeah, need more details, though the script can be improved (no need parantheses, use
-F
for fixed string matching, use single quotes for search argument, lowercase variable names, etc), the given bash snippet doesn't seem to have an error as such..– Sundeep
Nov 21 '18 at 15:03
yeah, need more details, though the script can be improved (no need parantheses, use
-F
for fixed string matching, use single quotes for search argument, lowercase variable names, etc), the given bash snippet doesn't seem to have an error as such..– Sundeep
Nov 21 '18 at 15:03
add a comment |
1 Answer
1
active
oldest
votes
You are attempting to check the exit code of grep ( which should be checked with $? instead ). Here I have rewritten your code to work :
#!/bin/bash
TEST_FILE='./input.txt'
[[ $( grep -i '[xyz]' "$TEST_FILE" ) ]] && PATTERN_MATCH="xyz matched"
[[ $( grep -i '[abc]' "$TEST_FILE" ) ]] && PATTERN_MATCH="abc matched"
echo $PATTERN_MATCH
Hope it helps buddy!
1
Probably you're not aware of-q
functionality ingrep
. There is no need to use command substitution here.
– anubhava
Nov 21 '18 at 15:13
I know what -q does. Can you please provide a sample of how to test the exit code without using $? ?
– Matias Barrios
Nov 21 '18 at 15:23
1
It's justif grep -q 'pattern' infile; then ...
orgrep -q 'pattern' infile && ...
.
– Benjamin W.
Nov 21 '18 at 15:35
@BenjaminW. First example foes not work on my system ( Centos 7); second one works though. Thanks!
– Matias Barrios
Nov 21 '18 at 15:59
1
That's weird,-q
is even required by POSIX. Definitely works for me (both GNU grep 3.1 and BSD grep 2.5.1).
– Benjamin W.
Nov 21 '18 at 16:01
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f53414790%2fmatch-content-by-reading-file-using-bash-grep-string-enclosed-with-and-spec%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You are attempting to check the exit code of grep ( which should be checked with $? instead ). Here I have rewritten your code to work :
#!/bin/bash
TEST_FILE='./input.txt'
[[ $( grep -i '[xyz]' "$TEST_FILE" ) ]] && PATTERN_MATCH="xyz matched"
[[ $( grep -i '[abc]' "$TEST_FILE" ) ]] && PATTERN_MATCH="abc matched"
echo $PATTERN_MATCH
Hope it helps buddy!
1
Probably you're not aware of-q
functionality ingrep
. There is no need to use command substitution here.
– anubhava
Nov 21 '18 at 15:13
I know what -q does. Can you please provide a sample of how to test the exit code without using $? ?
– Matias Barrios
Nov 21 '18 at 15:23
1
It's justif grep -q 'pattern' infile; then ...
orgrep -q 'pattern' infile && ...
.
– Benjamin W.
Nov 21 '18 at 15:35
@BenjaminW. First example foes not work on my system ( Centos 7); second one works though. Thanks!
– Matias Barrios
Nov 21 '18 at 15:59
1
That's weird,-q
is even required by POSIX. Definitely works for me (both GNU grep 3.1 and BSD grep 2.5.1).
– Benjamin W.
Nov 21 '18 at 16:01
add a comment |
You are attempting to check the exit code of grep ( which should be checked with $? instead ). Here I have rewritten your code to work :
#!/bin/bash
TEST_FILE='./input.txt'
[[ $( grep -i '[xyz]' "$TEST_FILE" ) ]] && PATTERN_MATCH="xyz matched"
[[ $( grep -i '[abc]' "$TEST_FILE" ) ]] && PATTERN_MATCH="abc matched"
echo $PATTERN_MATCH
Hope it helps buddy!
1
Probably you're not aware of-q
functionality ingrep
. There is no need to use command substitution here.
– anubhava
Nov 21 '18 at 15:13
I know what -q does. Can you please provide a sample of how to test the exit code without using $? ?
– Matias Barrios
Nov 21 '18 at 15:23
1
It's justif grep -q 'pattern' infile; then ...
orgrep -q 'pattern' infile && ...
.
– Benjamin W.
Nov 21 '18 at 15:35
@BenjaminW. First example foes not work on my system ( Centos 7); second one works though. Thanks!
– Matias Barrios
Nov 21 '18 at 15:59
1
That's weird,-q
is even required by POSIX. Definitely works for me (both GNU grep 3.1 and BSD grep 2.5.1).
– Benjamin W.
Nov 21 '18 at 16:01
add a comment |
You are attempting to check the exit code of grep ( which should be checked with $? instead ). Here I have rewritten your code to work :
#!/bin/bash
TEST_FILE='./input.txt'
[[ $( grep -i '[xyz]' "$TEST_FILE" ) ]] && PATTERN_MATCH="xyz matched"
[[ $( grep -i '[abc]' "$TEST_FILE" ) ]] && PATTERN_MATCH="abc matched"
echo $PATTERN_MATCH
Hope it helps buddy!
You are attempting to check the exit code of grep ( which should be checked with $? instead ). Here I have rewritten your code to work :
#!/bin/bash
TEST_FILE='./input.txt'
[[ $( grep -i '[xyz]' "$TEST_FILE" ) ]] && PATTERN_MATCH="xyz matched"
[[ $( grep -i '[abc]' "$TEST_FILE" ) ]] && PATTERN_MATCH="abc matched"
echo $PATTERN_MATCH
Hope it helps buddy!
answered Nov 21 '18 at 15:07
Matias BarriosMatias Barrios
1,542316
1,542316
1
Probably you're not aware of-q
functionality ingrep
. There is no need to use command substitution here.
– anubhava
Nov 21 '18 at 15:13
I know what -q does. Can you please provide a sample of how to test the exit code without using $? ?
– Matias Barrios
Nov 21 '18 at 15:23
1
It's justif grep -q 'pattern' infile; then ...
orgrep -q 'pattern' infile && ...
.
– Benjamin W.
Nov 21 '18 at 15:35
@BenjaminW. First example foes not work on my system ( Centos 7); second one works though. Thanks!
– Matias Barrios
Nov 21 '18 at 15:59
1
That's weird,-q
is even required by POSIX. Definitely works for me (both GNU grep 3.1 and BSD grep 2.5.1).
– Benjamin W.
Nov 21 '18 at 16:01
add a comment |
1
Probably you're not aware of-q
functionality ingrep
. There is no need to use command substitution here.
– anubhava
Nov 21 '18 at 15:13
I know what -q does. Can you please provide a sample of how to test the exit code without using $? ?
– Matias Barrios
Nov 21 '18 at 15:23
1
It's justif grep -q 'pattern' infile; then ...
orgrep -q 'pattern' infile && ...
.
– Benjamin W.
Nov 21 '18 at 15:35
@BenjaminW. First example foes not work on my system ( Centos 7); second one works though. Thanks!
– Matias Barrios
Nov 21 '18 at 15:59
1
That's weird,-q
is even required by POSIX. Definitely works for me (both GNU grep 3.1 and BSD grep 2.5.1).
– Benjamin W.
Nov 21 '18 at 16:01
1
1
Probably you're not aware of
-q
functionality in grep
. There is no need to use command substitution here.– anubhava
Nov 21 '18 at 15:13
Probably you're not aware of
-q
functionality in grep
. There is no need to use command substitution here.– anubhava
Nov 21 '18 at 15:13
I know what -q does. Can you please provide a sample of how to test the exit code without using $? ?
– Matias Barrios
Nov 21 '18 at 15:23
I know what -q does. Can you please provide a sample of how to test the exit code without using $? ?
– Matias Barrios
Nov 21 '18 at 15:23
1
1
It's just
if grep -q 'pattern' infile; then ...
or grep -q 'pattern' infile && ...
.– Benjamin W.
Nov 21 '18 at 15:35
It's just
if grep -q 'pattern' infile; then ...
or grep -q 'pattern' infile && ...
.– Benjamin W.
Nov 21 '18 at 15:35
@BenjaminW. First example foes not work on my system ( Centos 7); second one works though. Thanks!
– Matias Barrios
Nov 21 '18 at 15:59
@BenjaminW. First example foes not work on my system ( Centos 7); second one works though. Thanks!
– Matias Barrios
Nov 21 '18 at 15:59
1
1
That's weird,
-q
is even required by POSIX. Definitely works for me (both GNU grep 3.1 and BSD grep 2.5.1).– Benjamin W.
Nov 21 '18 at 16:01
That's weird,
-q
is even required by POSIX. Definitely works for me (both GNU grep 3.1 and BSD grep 2.5.1).– Benjamin W.
Nov 21 '18 at 16:01
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f53414790%2fmatch-content-by-reading-file-using-bash-grep-string-enclosed-with-and-spec%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
1
What is not working?
– anubhava
Nov 21 '18 at 15:01
2
yeah, need more details, though the script can be improved (no need parantheses, use
-F
for fixed string matching, use single quotes for search argument, lowercase variable names, etc), the given bash snippet doesn't seem to have an error as such..– Sundeep
Nov 21 '18 at 15:03