Match content by reading file using bash grep, string enclosed with [ and ] special character












1















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!










share|improve this question




















  • 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















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!










share|improve this question




















  • 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








1


1






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!










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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
















  • 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














1 Answer
1






active

oldest

votes


















2














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!






share|improve this answer



















  • 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











  • 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 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






  • 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











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
});


}
});














draft saved

draft discarded


















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









2














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!






share|improve this answer



















  • 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











  • 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 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






  • 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
















2














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!






share|improve this answer



















  • 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











  • 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 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






  • 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














2












2








2







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!






share|improve this answer













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!







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 15:07









Matias BarriosMatias Barrios

1,542316




1,542316








  • 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











  • 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 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






  • 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





    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






  • 1





    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






  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

Origin of the phrase “under your belt”?